regex_internal.h 25 KB

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