regex_internal.h 25 KB

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