regex_internal.h 25 KB

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