regex_internal.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  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. #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. #if (defined MB_CUR_MAX && HAVE_WCTYPE_H && HAVE_ISWCTYPE) || _LIBC
  104. # define RE_ENABLE_I18N
  105. #endif
  106. /* Number of ASCII characters. */
  107. #define ASCII_CHARS 0x80
  108. /* Number of single byte characters. */
  109. #define SBC_MAX (UCHAR_MAX + 1)
  110. #define COLL_ELEM_LEN_MAX 8
  111. /* The character which represents newline. */
  112. #define NEWLINE_CHAR '\n'
  113. #define WIDE_NEWLINE_CHAR L'\n'
  114. /* Rename to standard API for using out of glibc. */
  115. #ifndef _LIBC
  116. # undef __wctype
  117. # undef __iswalnum
  118. # undef __iswctype
  119. # undef __towlower
  120. # undef __towupper
  121. # define __wctype wctype
  122. # define __iswalnum iswalnum
  123. # define __iswctype iswctype
  124. # define __towlower towlower
  125. # define __towupper towupper
  126. # define __btowc btowc
  127. # define __mbrtowc mbrtowc
  128. # define __wcrtomb wcrtomb
  129. # define __regfree regfree
  130. #endif /* not _LIBC */
  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. #ifdef RE_ENABLE_I18N
  218. COMPLEX_BRACKET = 6,
  219. OP_UTF8_PERIOD = 7,
  220. #endif /* RE_ENABLE_I18N */
  221. /* We define EPSILON_BIT as a macro so that OP_OPEN_SUBEXP is used
  222. when the debugger shows values of this enum type. */
  223. #define EPSILON_BIT 8
  224. OP_OPEN_SUBEXP = EPSILON_BIT | 0,
  225. OP_CLOSE_SUBEXP = EPSILON_BIT | 1,
  226. OP_ALT = EPSILON_BIT | 2,
  227. OP_DUP_ASTERISK = EPSILON_BIT | 3,
  228. ANCHOR = EPSILON_BIT | 4,
  229. /* Tree type, these are used only by tree. */
  230. CONCAT = 16,
  231. SUBEXP = 17,
  232. /* Token type, these are used only by token. */
  233. OP_DUP_PLUS = 18,
  234. OP_DUP_QUESTION,
  235. OP_OPEN_BRACKET,
  236. OP_CLOSE_BRACKET,
  237. OP_CHARSET_RANGE,
  238. OP_OPEN_DUP_NUM,
  239. OP_CLOSE_DUP_NUM,
  240. OP_NON_MATCH_LIST,
  241. OP_OPEN_COLL_ELEM,
  242. OP_CLOSE_COLL_ELEM,
  243. OP_OPEN_EQUIV_CLASS,
  244. OP_CLOSE_EQUIV_CLASS,
  245. OP_OPEN_CHAR_CLASS,
  246. OP_CLOSE_CHAR_CLASS,
  247. OP_WORD,
  248. OP_NOTWORD,
  249. OP_SPACE,
  250. OP_NOTSPACE,
  251. BACK_SLASH
  252. } re_token_type_t;
  253. #ifdef RE_ENABLE_I18N
  254. typedef struct
  255. {
  256. /* Multibyte characters. */
  257. wchar_t *mbchars;
  258. /* Collating symbols. */
  259. # ifdef _LIBC
  260. int32_t *coll_syms;
  261. # endif
  262. /* Equivalence classes. */
  263. # ifdef _LIBC
  264. int32_t *equiv_classes;
  265. # endif
  266. /* Range expressions. */
  267. # ifdef _LIBC
  268. uint32_t *range_starts;
  269. uint32_t *range_ends;
  270. # else /* not _LIBC */
  271. wchar_t *range_starts;
  272. wchar_t *range_ends;
  273. # endif /* not _LIBC */
  274. /* Character classes. */
  275. wctype_t *char_classes;
  276. /* If this character set is the non-matching list. */
  277. unsigned int non_match : 1;
  278. /* # of multibyte characters. */
  279. Idx nmbchars;
  280. /* # of collating symbols. */
  281. Idx ncoll_syms;
  282. /* # of equivalence classes. */
  283. Idx nequiv_classes;
  284. /* # of range expressions. */
  285. Idx nranges;
  286. /* # of character classes. */
  287. Idx nchar_classes;
  288. } re_charset_t;
  289. #endif /* RE_ENABLE_I18N */
  290. typedef struct
  291. {
  292. union
  293. {
  294. unsigned char c; /* for CHARACTER */
  295. re_bitset_ptr_t sbcset; /* for SIMPLE_BRACKET */
  296. #ifdef RE_ENABLE_I18N
  297. re_charset_t *mbcset; /* for COMPLEX_BRACKET */
  298. #endif /* RE_ENABLE_I18N */
  299. Idx idx; /* for BACK_REF */
  300. re_context_type ctx_type; /* for ANCHOR */
  301. } opr;
  302. #if (__GNUC__ >= 2 || defined __clang__) && !defined __STRICT_ANSI__
  303. re_token_type_t type : 8;
  304. #else
  305. re_token_type_t type;
  306. #endif
  307. unsigned int constraint : 10; /* context constraint */
  308. unsigned int duplicated : 1;
  309. unsigned int opt_subexp : 1;
  310. #ifdef RE_ENABLE_I18N
  311. unsigned int accept_mb : 1;
  312. /* These 2 bits can be moved into the union if needed (e.g. if running out
  313. of bits; move opr.c to opr.c.c and move the flags to opr.c.flags). */
  314. unsigned int mb_partial : 1;
  315. #endif
  316. unsigned int word_char : 1;
  317. } re_token_t;
  318. #define IS_EPSILON_NODE(type) ((type) & EPSILON_BIT)
  319. struct re_string_t
  320. {
  321. /* Indicate the raw buffer which is the original string passed as an
  322. argument of regexec(), re_search(), etc.. */
  323. const unsigned char *raw_mbs;
  324. /* Store the multibyte string. In case of "case insensitive mode" like
  325. REG_ICASE, upper cases of the string are stored, otherwise MBS points
  326. the same address that RAW_MBS points. */
  327. unsigned char *mbs;
  328. #ifdef RE_ENABLE_I18N
  329. /* Store the wide character string which is corresponding to MBS. */
  330. wint_t *wcs;
  331. Idx *offsets;
  332. mbstate_t cur_state;
  333. #endif
  334. /* Index in RAW_MBS. Each character mbs[i] corresponds to
  335. raw_mbs[raw_mbs_idx + i]. */
  336. Idx raw_mbs_idx;
  337. /* The length of the valid characters in the buffers. */
  338. Idx valid_len;
  339. /* The corresponding number of bytes in raw_mbs array. */
  340. Idx valid_raw_len;
  341. /* The length of the buffers MBS and WCS. */
  342. Idx bufs_len;
  343. /* The index in MBS, which is updated by re_string_fetch_byte. */
  344. Idx cur_idx;
  345. /* length of RAW_MBS array. */
  346. Idx raw_len;
  347. /* This is RAW_LEN - RAW_MBS_IDX + VALID_LEN - VALID_RAW_LEN. */
  348. Idx len;
  349. /* End of the buffer may be shorter than its length in the cases such
  350. as re_match_2, re_search_2. Then, we use STOP for end of the buffer
  351. instead of LEN. */
  352. Idx raw_stop;
  353. /* This is RAW_STOP - RAW_MBS_IDX adjusted through OFFSETS. */
  354. Idx stop;
  355. /* The context of mbs[0]. We store the context independently, since
  356. the context of mbs[0] may be different from raw_mbs[0], which is
  357. the beginning of the input string. */
  358. unsigned int tip_context;
  359. /* The translation passed as a part of an argument of re_compile_pattern. */
  360. RE_TRANSLATE_TYPE trans;
  361. /* Copy of re_dfa_t's word_char. */
  362. re_const_bitset_ptr_t word_char;
  363. /* true if REG_ICASE. */
  364. unsigned char icase;
  365. unsigned char is_utf8;
  366. unsigned char map_notascii;
  367. unsigned char mbs_allocated;
  368. unsigned char offsets_needed;
  369. unsigned char newline_anchor;
  370. unsigned char word_ops_used;
  371. int mb_cur_max;
  372. };
  373. typedef struct re_string_t re_string_t;
  374. struct re_dfa_t;
  375. typedef struct re_dfa_t re_dfa_t;
  376. #ifndef _LIBC
  377. # define IS_IN(libc) false
  378. #endif
  379. #define re_string_peek_byte(pstr, offset) \
  380. ((pstr)->mbs[(pstr)->cur_idx + offset])
  381. #define re_string_fetch_byte(pstr) \
  382. ((pstr)->mbs[(pstr)->cur_idx++])
  383. #define re_string_first_byte(pstr, idx) \
  384. ((idx) == (pstr)->valid_len || (pstr)->wcs[idx] != WEOF)
  385. #define re_string_is_single_byte_char(pstr, idx) \
  386. ((pstr)->wcs[idx] != WEOF && ((pstr)->valid_len == (idx) + 1 \
  387. || (pstr)->wcs[(idx) + 1] != WEOF))
  388. #define re_string_eoi(pstr) ((pstr)->stop <= (pstr)->cur_idx)
  389. #define re_string_cur_idx(pstr) ((pstr)->cur_idx)
  390. #define re_string_get_buffer(pstr) ((pstr)->mbs)
  391. #define re_string_length(pstr) ((pstr)->len)
  392. #define re_string_byte_at(pstr,idx) ((pstr)->mbs[idx])
  393. #define re_string_skip_bytes(pstr,idx) ((pstr)->cur_idx += (idx))
  394. #define re_string_set_index(pstr,idx) ((pstr)->cur_idx = (idx))
  395. #ifdef _LIBC
  396. # define MALLOC_0_IS_NONNULL 1
  397. #elif !defined MALLOC_0_IS_NONNULL
  398. # define MALLOC_0_IS_NONNULL 0
  399. #endif
  400. #ifndef MAX
  401. # define MAX(a,b) ((a) < (b) ? (b) : (a))
  402. #endif
  403. #ifndef MIN
  404. # define MIN(a,b) ((a) < (b) ? (a) : (b))
  405. #endif
  406. #define re_malloc(t,n) ((t *) malloc ((n) * sizeof (t)))
  407. #define re_realloc(p,t,n) ((t *) realloc (p, (n) * sizeof (t)))
  408. #define re_free(p) free (p)
  409. struct bin_tree_t
  410. {
  411. struct bin_tree_t *parent;
  412. struct bin_tree_t *left;
  413. struct bin_tree_t *right;
  414. struct bin_tree_t *first;
  415. struct bin_tree_t *next;
  416. re_token_t token;
  417. /* 'node_idx' is the index in dfa->nodes, if 'type' == 0.
  418. Otherwise 'type' indicate the type of this node. */
  419. Idx node_idx;
  420. };
  421. typedef struct bin_tree_t bin_tree_t;
  422. #define BIN_TREE_STORAGE_SIZE \
  423. ((1024 - sizeof (void *)) / sizeof (bin_tree_t))
  424. struct bin_tree_storage_t
  425. {
  426. struct bin_tree_storage_t *next;
  427. bin_tree_t data[BIN_TREE_STORAGE_SIZE];
  428. };
  429. typedef struct bin_tree_storage_t bin_tree_storage_t;
  430. #define CONTEXT_WORD 1
  431. #define CONTEXT_NEWLINE (CONTEXT_WORD << 1)
  432. #define CONTEXT_BEGBUF (CONTEXT_NEWLINE << 1)
  433. #define CONTEXT_ENDBUF (CONTEXT_BEGBUF << 1)
  434. #define IS_WORD_CONTEXT(c) ((c) & CONTEXT_WORD)
  435. #define IS_NEWLINE_CONTEXT(c) ((c) & CONTEXT_NEWLINE)
  436. #define IS_BEGBUF_CONTEXT(c) ((c) & CONTEXT_BEGBUF)
  437. #define IS_ENDBUF_CONTEXT(c) ((c) & CONTEXT_ENDBUF)
  438. #define IS_ORDINARY_CONTEXT(c) ((c) == 0)
  439. #define IS_WORD_CHAR(ch) (isalnum (ch) || (ch) == '_')
  440. #define IS_NEWLINE(ch) ((ch) == NEWLINE_CHAR)
  441. #define IS_WIDE_WORD_CHAR(ch) (__iswalnum (ch) || (ch) == L'_')
  442. #define IS_WIDE_NEWLINE(ch) ((ch) == WIDE_NEWLINE_CHAR)
  443. #define NOT_SATISFY_PREV_CONSTRAINT(constraint,context) \
  444. ((((constraint) & PREV_WORD_CONSTRAINT) && !IS_WORD_CONTEXT (context)) \
  445. || ((constraint & PREV_NOTWORD_CONSTRAINT) && IS_WORD_CONTEXT (context)) \
  446. || ((constraint & PREV_NEWLINE_CONSTRAINT) && !IS_NEWLINE_CONTEXT (context))\
  447. || ((constraint & PREV_BEGBUF_CONSTRAINT) && !IS_BEGBUF_CONTEXT (context)))
  448. #define NOT_SATISFY_NEXT_CONSTRAINT(constraint,context) \
  449. ((((constraint) & NEXT_WORD_CONSTRAINT) && !IS_WORD_CONTEXT (context)) \
  450. || (((constraint) & NEXT_NOTWORD_CONSTRAINT) && IS_WORD_CONTEXT (context)) \
  451. || (((constraint) & NEXT_NEWLINE_CONSTRAINT) && !IS_NEWLINE_CONTEXT (context)) \
  452. || (((constraint) & NEXT_ENDBUF_CONSTRAINT) && !IS_ENDBUF_CONTEXT (context)))
  453. struct re_dfastate_t
  454. {
  455. re_hashval_t hash;
  456. re_node_set nodes;
  457. re_node_set non_eps_nodes;
  458. re_node_set inveclosure;
  459. re_node_set *entrance_nodes;
  460. struct re_dfastate_t **trtable, **word_trtable;
  461. unsigned int context : 4;
  462. unsigned int halt : 1;
  463. /* If this state can accept "multi byte".
  464. Note that we refer to multibyte characters, and multi character
  465. collating elements as "multi byte". */
  466. unsigned int accept_mb : 1;
  467. /* If this state has backreference node(s). */
  468. unsigned int has_backref : 1;
  469. unsigned int has_constraint : 1;
  470. };
  471. typedef struct re_dfastate_t re_dfastate_t;
  472. struct re_state_table_entry
  473. {
  474. Idx num;
  475. Idx alloc;
  476. re_dfastate_t **array;
  477. };
  478. /* Array type used in re_sub_match_last_t and re_sub_match_top_t. */
  479. typedef struct
  480. {
  481. Idx next_idx;
  482. Idx alloc;
  483. re_dfastate_t **array;
  484. } state_array_t;
  485. /* Store information about the node NODE whose type is OP_CLOSE_SUBEXP. */
  486. typedef struct
  487. {
  488. Idx node;
  489. Idx str_idx; /* The position NODE match at. */
  490. state_array_t path;
  491. } re_sub_match_last_t;
  492. /* Store information about the node NODE whose type is OP_OPEN_SUBEXP.
  493. And information about the node, whose type is OP_CLOSE_SUBEXP,
  494. corresponding to NODE is stored in LASTS. */
  495. typedef struct
  496. {
  497. Idx str_idx;
  498. Idx node;
  499. state_array_t *path;
  500. Idx alasts; /* Allocation size of LASTS. */
  501. Idx nlasts; /* The number of LASTS. */
  502. re_sub_match_last_t **lasts;
  503. } re_sub_match_top_t;
  504. struct re_backref_cache_entry
  505. {
  506. Idx node;
  507. Idx str_idx;
  508. Idx subexp_from;
  509. Idx subexp_to;
  510. bitset_word_t eps_reachable_subexps_map;
  511. char more;
  512. };
  513. typedef struct
  514. {
  515. /* The string object corresponding to the input string. */
  516. re_string_t input;
  517. const re_dfa_t *const dfa;
  518. /* EFLAGS of the argument of regexec. */
  519. int eflags;
  520. /* Where the matching ends. */
  521. Idx match_last;
  522. Idx last_node;
  523. /* The state log used by the matcher. */
  524. re_dfastate_t **state_log;
  525. Idx state_log_top;
  526. /* Back reference cache. */
  527. Idx nbkref_ents;
  528. Idx abkref_ents;
  529. struct re_backref_cache_entry *bkref_ents;
  530. int max_mb_elem_len;
  531. Idx nsub_tops;
  532. Idx asub_tops;
  533. re_sub_match_top_t **sub_tops;
  534. } re_match_context_t;
  535. typedef struct
  536. {
  537. re_dfastate_t **sifted_states;
  538. re_dfastate_t **limited_states;
  539. Idx last_node;
  540. Idx last_str_idx;
  541. re_node_set limits;
  542. } re_sift_context_t;
  543. struct re_fail_stack_ent_t
  544. {
  545. Idx idx;
  546. Idx node;
  547. regmatch_t *regs;
  548. re_node_set eps_via_nodes;
  549. };
  550. struct re_fail_stack_t
  551. {
  552. Idx num;
  553. Idx alloc;
  554. struct re_fail_stack_ent_t *stack;
  555. };
  556. struct re_dfa_t
  557. {
  558. re_token_t *nodes;
  559. size_t nodes_alloc;
  560. size_t nodes_len;
  561. Idx *nexts;
  562. Idx *org_indices;
  563. re_node_set *edests;
  564. re_node_set *eclosures;
  565. re_node_set *inveclosures;
  566. struct re_state_table_entry *state_table;
  567. re_dfastate_t *init_state;
  568. re_dfastate_t *init_state_word;
  569. re_dfastate_t *init_state_nl;
  570. re_dfastate_t *init_state_begbuf;
  571. bin_tree_t *str_tree;
  572. bin_tree_storage_t *str_tree_storage;
  573. re_bitset_ptr_t sb_char;
  574. int str_tree_storage_idx;
  575. /* number of subexpressions 're_nsub' is in regex_t. */
  576. re_hashval_t state_hash_mask;
  577. Idx init_node;
  578. Idx nbackref; /* The number of backreference in this dfa. */
  579. /* Bitmap expressing which backreference is used. */
  580. bitset_word_t used_bkref_map;
  581. bitset_word_t completed_bkref_map;
  582. unsigned int has_plural_match : 1;
  583. /* If this dfa has "multibyte node", which is a backreference or
  584. a node which can accept multibyte character or multi character
  585. collating element. */
  586. unsigned int has_mb_node : 1;
  587. unsigned int is_utf8 : 1;
  588. unsigned int map_notascii : 1;
  589. unsigned int word_ops_used : 1;
  590. int mb_cur_max;
  591. bitset_t word_char;
  592. reg_syntax_t syntax;
  593. Idx *subexp_map;
  594. #ifdef DEBUG
  595. char* re_str;
  596. #endif
  597. lock_define (lock)
  598. };
  599. #define re_node_set_init_empty(set) memset (set, '\0', sizeof (re_node_set))
  600. #define re_node_set_remove(set,id) \
  601. (re_node_set_remove_at (set, re_node_set_contains (set, id) - 1))
  602. #define re_node_set_empty(p) ((p)->nelem = 0)
  603. #define re_node_set_free(set) re_free ((set)->elems)
  604. typedef enum
  605. {
  606. SB_CHAR,
  607. MB_CHAR,
  608. EQUIV_CLASS,
  609. COLL_SYM,
  610. CHAR_CLASS
  611. } bracket_elem_type;
  612. typedef struct
  613. {
  614. bracket_elem_type type;
  615. union
  616. {
  617. unsigned char ch;
  618. unsigned char *name;
  619. wchar_t wch;
  620. } opr;
  621. } bracket_elem_t;
  622. /* Functions for bitset_t operation. */
  623. static inline void
  624. bitset_set (bitset_t set, Idx i)
  625. {
  626. set[i / BITSET_WORD_BITS] |= (bitset_word_t) 1 << i % BITSET_WORD_BITS;
  627. }
  628. static inline void
  629. bitset_clear (bitset_t set, Idx i)
  630. {
  631. set[i / BITSET_WORD_BITS] &= ~ ((bitset_word_t) 1 << i % BITSET_WORD_BITS);
  632. }
  633. static inline bool
  634. bitset_contain (const bitset_t set, Idx i)
  635. {
  636. return (set[i / BITSET_WORD_BITS] >> i % BITSET_WORD_BITS) & 1;
  637. }
  638. static inline void
  639. bitset_empty (bitset_t set)
  640. {
  641. memset (set, '\0', sizeof (bitset_t));
  642. }
  643. static inline void
  644. bitset_set_all (bitset_t set)
  645. {
  646. memset (set, -1, sizeof (bitset_word_t) * (SBC_MAX / BITSET_WORD_BITS));
  647. if (SBC_MAX % BITSET_WORD_BITS != 0)
  648. set[BITSET_WORDS - 1] =
  649. ((bitset_word_t) 1 << SBC_MAX % BITSET_WORD_BITS) - 1;
  650. }
  651. static inline void
  652. bitset_copy (bitset_t dest, const bitset_t src)
  653. {
  654. memcpy (dest, src, sizeof (bitset_t));
  655. }
  656. static inline void
  657. bitset_not (bitset_t set)
  658. {
  659. int bitset_i;
  660. for (bitset_i = 0; bitset_i < SBC_MAX / BITSET_WORD_BITS; ++bitset_i)
  661. set[bitset_i] = ~set[bitset_i];
  662. if (SBC_MAX % BITSET_WORD_BITS != 0)
  663. set[BITSET_WORDS - 1] =
  664. ((((bitset_word_t) 1 << SBC_MAX % BITSET_WORD_BITS) - 1)
  665. & ~set[BITSET_WORDS - 1]);
  666. }
  667. static inline void
  668. bitset_merge (bitset_t dest, const bitset_t src)
  669. {
  670. int bitset_i;
  671. for (bitset_i = 0; bitset_i < BITSET_WORDS; ++bitset_i)
  672. dest[bitset_i] |= src[bitset_i];
  673. }
  674. static inline void
  675. bitset_mask (bitset_t dest, const bitset_t src)
  676. {
  677. int bitset_i;
  678. for (bitset_i = 0; bitset_i < BITSET_WORDS; ++bitset_i)
  679. dest[bitset_i] &= src[bitset_i];
  680. }
  681. #ifdef RE_ENABLE_I18N
  682. /* Functions for re_string. */
  683. static int
  684. __attribute__ ((pure, unused))
  685. re_string_char_size_at (const re_string_t *pstr, Idx idx)
  686. {
  687. int byte_idx;
  688. if (pstr->mb_cur_max == 1)
  689. return 1;
  690. for (byte_idx = 1; idx + byte_idx < pstr->valid_len; ++byte_idx)
  691. if (pstr->wcs[idx + byte_idx] != WEOF)
  692. break;
  693. return byte_idx;
  694. }
  695. static wint_t
  696. __attribute__ ((pure, unused))
  697. re_string_wchar_at (const re_string_t *pstr, Idx idx)
  698. {
  699. if (pstr->mb_cur_max == 1)
  700. return (wint_t) pstr->mbs[idx];
  701. return (wint_t) pstr->wcs[idx];
  702. }
  703. # ifdef _LIBC
  704. # include <locale/weight.h>
  705. # endif
  706. static int
  707. __attribute__ ((pure, unused))
  708. re_string_elem_size_at (const re_string_t *pstr, Idx idx)
  709. {
  710. # ifdef _LIBC
  711. const unsigned char *p, *extra;
  712. const int32_t *table, *indirect;
  713. uint_fast32_t nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
  714. if (nrules != 0)
  715. {
  716. table = (const int32_t *) _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB);
  717. extra = (const unsigned char *)
  718. _NL_CURRENT (LC_COLLATE, _NL_COLLATE_EXTRAMB);
  719. indirect = (const int32_t *) _NL_CURRENT (LC_COLLATE,
  720. _NL_COLLATE_INDIRECTMB);
  721. p = pstr->mbs + idx;
  722. findidx (table, indirect, extra, &p, pstr->len - idx);
  723. return p - pstr->mbs - idx;
  724. }
  725. else
  726. # endif /* _LIBC */
  727. return 1;
  728. }
  729. #endif /* RE_ENABLE_I18N */
  730. #ifdef _LIBC
  731. # if __GNUC__ >= 7
  732. # define FALLTHROUGH __attribute__ ((__fallthrough__))
  733. # else
  734. # define FALLTHROUGH ((void) 0)
  735. # endif
  736. #else
  737. # include "attribute.h"
  738. #endif
  739. #endif /* _REGEX_INTERNAL_H */