regex_internal.h 25 KB

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