PageRenderTime 68ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 2ms

/LotsaGlass/GLObject/Data management/regex.c

https://code.google.com/p/lotsablankers/
C | 10689 lines | 8466 code | 922 blank | 1301 comment | 2532 complexity | 7cd64486ca5a35e4d4301e2360a0a241 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. /* Extended regular expression matching and search library.
  2. Copyright (C) 2002, 2003, 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. 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, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  17. #ifdef HAVE_CONFIG_H
  18. #include "config.h"
  19. #endif
  20. #include <stdbool.h>
  21. #define __USE_GNU
  22. /* Make sure noone compiles this code with a C++ compiler. */
  23. #ifdef __cplusplus
  24. # error "This is C code, use a C compiler"
  25. #endif
  26. #ifdef _LIBC
  27. /* We have to keep the namespace clean. */
  28. # define regfree(preg) __regfree (preg)
  29. # define regexec(pr, st, nm, pm, ef) __regexec (pr, st, nm, pm, ef)
  30. # define regcomp(preg, pattern, cflags) __regcomp (preg, pattern, cflags)
  31. # define regerror(errcode, preg, errbuf, errbuf_size) \
  32. __regerror(errcode, preg, errbuf, errbuf_size)
  33. # define re_set_registers(bu, re, nu, st, en) \
  34. __re_set_registers (bu, re, nu, st, en)
  35. # define re_match_2(bufp, string1, size1, string2, size2, pos, regs, stop) \
  36. __re_match_2 (bufp, string1, size1, string2, size2, pos, regs, stop)
  37. # define re_match(bufp, string, size, pos, regs) \
  38. __re_match (bufp, string, size, pos, regs)
  39. # define re_search(bufp, string, size, startpos, range, regs) \
  40. __re_search (bufp, string, size, startpos, range, regs)
  41. # define re_compile_pattern(pattern, length, bufp) \
  42. __re_compile_pattern (pattern, length, bufp)
  43. # define re_set_syntax(syntax) __re_set_syntax (syntax)
  44. # define re_search_2(bufp, st1, s1, st2, s2, startpos, range, regs, stop) \
  45. __re_search_2 (bufp, st1, s1, st2, s2, startpos, range, regs, stop)
  46. # define re_compile_fastmap(bufp) __re_compile_fastmap (bufp)
  47. # include "../locale/localeinfo.h"
  48. #endif
  49. /* On some systems, limits.h sets RE_DUP_MAX to a lower value than
  50. GNU regex allows. Include it before <regex.h>, which correctly
  51. #undefs RE_DUP_MAX and sets it to the right value. */
  52. #include <limits.h>
  53. #include "regex.h"
  54. /* Extended regular expression matching and search library.
  55. Copyright (C) 2002, 2003, 2004, 2005, 2007 Free Software Foundation, Inc.
  56. This file is part of the GNU C Library.
  57. Contributed by Isamu Hasegawa <isamu@yamato.ibm.com>.
  58. The GNU C Library is free software; you can redistribute it and/or
  59. modify it under the terms of the GNU Lesser General Public
  60. License as published by the Free Software Foundation; either
  61. version 2.1 of the License, or (at your option) any later version.
  62. The GNU C Library is distributed in the hope that it will be useful,
  63. but WITHOUT ANY WARRANTY; without even the implied warranty of
  64. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  65. Lesser General Public License for more details.
  66. You should have received a copy of the GNU Lesser General Public
  67. License along with the GNU C Library; if not, write to the Free
  68. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  69. 02111-1307 USA. */
  70. #ifndef _REGEX_INTERNAL_H
  71. #define _REGEX_INTERNAL_H 1
  72. #include <assert.h>
  73. #include <ctype.h>
  74. #include <stdio.h>
  75. #include <stdlib.h>
  76. #include <string.h>
  77. #if defined HAVE_LANGINFO_H || defined HAVE_LANGINFO_CODESET || defined _LIBC
  78. # include <langinfo.h>
  79. #endif
  80. #if defined HAVE_LOCALE_H || defined _LIBC
  81. # include <locale.h>
  82. #endif
  83. #if defined HAVE_WCHAR_H || defined _LIBC
  84. # include <wchar.h>
  85. #endif /* HAVE_WCHAR_H || _LIBC */
  86. #if defined HAVE_WCTYPE_H || defined _LIBC
  87. # include <wctype.h>
  88. #endif /* HAVE_WCTYPE_H || _LIBC */
  89. #if defined HAVE_STDBOOL_H || defined _LIBC
  90. # include <stdbool.h>
  91. #endif /* HAVE_STDBOOL_H || _LIBC */
  92. #if defined HAVE_STDINT_H || defined _LIBC
  93. # include <stdint.h>
  94. #endif /* HAVE_STDINT_H || _LIBC */
  95. #if defined _LIBC
  96. # include <bits/libc-lock.h>
  97. #else
  98. # define __libc_lock_define(CLASS,NAME)
  99. # define __libc_lock_init(NAME) do { } while (0)
  100. # define __libc_lock_lock(NAME) do { } while (0)
  101. # define __libc_lock_unlock(NAME) do { } while (0)
  102. #endif
  103. /* In case that the system doesn't have isblank(). */
  104. #if !defined _LIBC && !defined HAVE_ISBLANK && !defined isblank
  105. # define isblank(ch) ((ch) == ' ' || (ch) == '\t')
  106. #endif
  107. #ifdef _LIBC
  108. # ifndef _RE_DEFINE_LOCALE_FUNCTIONS
  109. # define _RE_DEFINE_LOCALE_FUNCTIONS 1
  110. # include <locale/localeinfo.h>
  111. # include <locale/elem-hash.h>
  112. # include <locale/coll-lookup.h>
  113. # endif
  114. #endif
  115. /* This is for other GNU distributions with internationalized messages. */
  116. #if (HAVE_LIBINTL_H && ENABLE_NLS) || defined _LIBC
  117. # include <libintl.h>
  118. # ifdef _LIBC
  119. # undef gettext
  120. # define gettext(msgid) \
  121. INTUSE(__dcgettext) (_libc_intl_domainname, msgid, LC_MESSAGES)
  122. # endif
  123. #else
  124. # define gettext(msgid) (msgid)
  125. #endif
  126. #ifndef gettext_noop
  127. /* This define is so xgettext can find the internationalizable
  128. strings. */
  129. # define gettext_noop(String) String
  130. #endif
  131. /* For loser systems without the definition. */
  132. #ifndef SIZE_MAX
  133. # define SIZE_MAX ((size_t) -1)
  134. #endif
  135. #if (defined MB_CUR_MAX && HAVE_LOCALE_H && HAVE_WCTYPE_H && HAVE_WCHAR_H && HAVE_WCRTOMB && HAVE_MBRTOWC && HAVE_WCSCOLL) || _LIBC
  136. # define RE_ENABLE_I18N
  137. #endif
  138. #if __GNUC__ >= 3
  139. # define BE(expr, val) __builtin_expect (expr, val)
  140. #else
  141. # define BE(expr, val) (expr)
  142. # define inline
  143. #endif
  144. /* Number of single byte character. */
  145. #define SBC_MAX 256
  146. #define COLL_ELEM_LEN_MAX 8
  147. /* The character which represents newline. */
  148. #define NEWLINE_CHAR '\n'
  149. #define WIDE_NEWLINE_CHAR L'\n'
  150. /* Rename to standard API for using out of glibc. */
  151. #ifndef _LIBC
  152. # define __wctype wctype
  153. # define __iswctype iswctype
  154. # define __btowc btowc
  155. # define __mempcpy mempcpy
  156. # define __wcrtomb wcrtomb
  157. # define __regfree regfree
  158. # define attribute_hidden
  159. #endif /* not _LIBC */
  160. #ifdef __GNUC__
  161. # define __attribute(arg) __attribute__ (arg)
  162. #else
  163. # define __attribute(arg)
  164. #endif
  165. extern const char __re_error_msgid[] attribute_hidden;
  166. extern const size_t __re_error_msgid_idx[] attribute_hidden;
  167. /* An integer used to represent a set of bits. It must be unsigned,
  168. and must be at least as wide as unsigned int. */
  169. typedef unsigned long int bitset_word_t;
  170. /* All bits set in a bitset_word_t. */
  171. #define BITSET_WORD_MAX ULONG_MAX
  172. /* Number of bits in a bitset_word_t. */
  173. #define BITSET_WORD_BITS (sizeof (bitset_word_t) * CHAR_BIT)
  174. /* Number of bitset_word_t in a bit_set. */
  175. #define BITSET_WORDS (SBC_MAX / BITSET_WORD_BITS)
  176. typedef bitset_word_t bitset_t[BITSET_WORDS];
  177. typedef bitset_word_t *re_bitset_ptr_t;
  178. typedef const bitset_word_t *re_const_bitset_ptr_t;
  179. #define bitset_set(set,i) \
  180. (set[i / BITSET_WORD_BITS] |= (bitset_word_t) 1 << i % BITSET_WORD_BITS)
  181. #define bitset_clear(set,i) \
  182. (set[i / BITSET_WORD_BITS] &= ~((bitset_word_t) 1 << i % BITSET_WORD_BITS))
  183. #define bitset_contain(set,i) \
  184. (set[i / BITSET_WORD_BITS] & ((bitset_word_t) 1 << i % BITSET_WORD_BITS))
  185. #define bitset_empty(set) memset (set, '\0', sizeof (bitset_t))
  186. #define bitset_set_all(set) memset (set, '\xff', sizeof (bitset_t))
  187. #define bitset_copy(dest,src) memcpy (dest, src, sizeof (bitset_t))
  188. #define PREV_WORD_CONSTRAINT 0x0001
  189. #define PREV_NOTWORD_CONSTRAINT 0x0002
  190. #define NEXT_WORD_CONSTRAINT 0x0004
  191. #define NEXT_NOTWORD_CONSTRAINT 0x0008
  192. #define PREV_NEWLINE_CONSTRAINT 0x0010
  193. #define NEXT_NEWLINE_CONSTRAINT 0x0020
  194. #define PREV_BEGBUF_CONSTRAINT 0x0040
  195. #define NEXT_ENDBUF_CONSTRAINT 0x0080
  196. #define WORD_DELIM_CONSTRAINT 0x0100
  197. #define NOT_WORD_DELIM_CONSTRAINT 0x0200
  198. typedef enum
  199. {
  200. INSIDE_WORD = PREV_WORD_CONSTRAINT | NEXT_WORD_CONSTRAINT,
  201. WORD_FIRST = PREV_NOTWORD_CONSTRAINT | NEXT_WORD_CONSTRAINT,
  202. WORD_LAST = PREV_WORD_CONSTRAINT | NEXT_NOTWORD_CONSTRAINT,
  203. INSIDE_NOTWORD = PREV_NOTWORD_CONSTRAINT | NEXT_NOTWORD_CONSTRAINT,
  204. LINE_FIRST = PREV_NEWLINE_CONSTRAINT,
  205. LINE_LAST = NEXT_NEWLINE_CONSTRAINT,
  206. BUF_FIRST = PREV_BEGBUF_CONSTRAINT,
  207. BUF_LAST = NEXT_ENDBUF_CONSTRAINT,
  208. WORD_DELIM = WORD_DELIM_CONSTRAINT,
  209. NOT_WORD_DELIM = NOT_WORD_DELIM_CONSTRAINT
  210. } re_context_type;
  211. typedef struct
  212. {
  213. int alloc;
  214. int nelem;
  215. int *elems;
  216. } re_node_set;
  217. typedef enum
  218. {
  219. NON_TYPE = 0,
  220. /* Node type, These are used by token, node, tree. */
  221. CHARACTER = 1,
  222. END_OF_RE = 2,
  223. SIMPLE_BRACKET = 3,
  224. OP_BACK_REF = 4,
  225. OP_PERIOD = 5,
  226. #ifdef RE_ENABLE_I18N
  227. COMPLEX_BRACKET = 6,
  228. OP_UTF8_PERIOD = 7,
  229. #endif /* RE_ENABLE_I18N */
  230. /* We define EPSILON_BIT as a macro so that OP_OPEN_SUBEXP is used
  231. when the debugger shows values of this enum type. */
  232. #define EPSILON_BIT 8
  233. OP_OPEN_SUBEXP = EPSILON_BIT | 0,
  234. OP_CLOSE_SUBEXP = EPSILON_BIT | 1,
  235. OP_ALT = EPSILON_BIT | 2,
  236. OP_DUP_ASTERISK = EPSILON_BIT | 3,
  237. ANCHOR = EPSILON_BIT | 4,
  238. /* Tree type, these are used only by tree. */
  239. CONCAT = 16,
  240. SUBEXP = 17,
  241. /* Token type, these are used only by token. */
  242. OP_DUP_PLUS = 18,
  243. OP_DUP_QUESTION,
  244. OP_OPEN_BRACKET,
  245. OP_CLOSE_BRACKET,
  246. OP_CHARSET_RANGE,
  247. OP_OPEN_DUP_NUM,
  248. OP_CLOSE_DUP_NUM,
  249. OP_NON_MATCH_LIST,
  250. OP_OPEN_COLL_ELEM,
  251. OP_CLOSE_COLL_ELEM,
  252. OP_OPEN_EQUIV_CLASS,
  253. OP_CLOSE_EQUIV_CLASS,
  254. OP_OPEN_CHAR_CLASS,
  255. OP_CLOSE_CHAR_CLASS,
  256. OP_WORD,
  257. OP_NOTWORD,
  258. OP_SPACE,
  259. OP_NOTSPACE,
  260. BACK_SLASH
  261. } re_token_type_t;
  262. #ifdef RE_ENABLE_I18N
  263. typedef struct
  264. {
  265. /* Multibyte characters. */
  266. wchar_t *mbchars;
  267. /* Collating symbols. */
  268. # ifdef _LIBC
  269. int32_t *coll_syms;
  270. # endif
  271. /* Equivalence classes. */
  272. # ifdef _LIBC
  273. int32_t *equiv_classes;
  274. # endif
  275. /* Range expressions. */
  276. # ifdef _LIBC
  277. uint32_t *range_starts;
  278. uint32_t *range_ends;
  279. # else /* not _LIBC */
  280. wchar_t *range_starts;
  281. wchar_t *range_ends;
  282. # endif /* not _LIBC */
  283. /* Character classes. */
  284. wctype_t *char_classes;
  285. /* If this character set is the non-matching list. */
  286. unsigned int non_match : 1;
  287. /* # of multibyte characters. */
  288. int nmbchars;
  289. /* # of collating symbols. */
  290. int ncoll_syms;
  291. /* # of equivalence classes. */
  292. int nequiv_classes;
  293. /* # of range expressions. */
  294. int nranges;
  295. /* # of character classes. */
  296. int nchar_classes;
  297. } re_charset_t;
  298. #endif /* RE_ENABLE_I18N */
  299. typedef struct
  300. {
  301. union
  302. {
  303. unsigned char c; /* for CHARACTER */
  304. re_bitset_ptr_t sbcset; /* for SIMPLE_BRACKET */
  305. #ifdef RE_ENABLE_I18N
  306. re_charset_t *mbcset; /* for COMPLEX_BRACKET */
  307. #endif /* RE_ENABLE_I18N */
  308. int idx; /* for BACK_REF */
  309. re_context_type ctx_type; /* for ANCHOR */
  310. } opr;
  311. #if __GNUC__ >= 2
  312. re_token_type_t type : 8;
  313. #else
  314. re_token_type_t type;
  315. #endif
  316. unsigned int constraint : 10; /* context constraint */
  317. unsigned int duplicated : 1;
  318. unsigned int opt_subexp : 1;
  319. #ifdef RE_ENABLE_I18N
  320. unsigned int accept_mb : 1;
  321. /* These 2 bits can be moved into the union if needed (e.g. if running out
  322. of bits; move opr.c to opr.c.c and move the flags to opr.c.flags). */
  323. unsigned int mb_partial : 1;
  324. #endif
  325. unsigned int word_char : 1;
  326. } re_token_t;
  327. #define IS_EPSILON_NODE(type) ((type) & EPSILON_BIT)
  328. struct re_string_t
  329. {
  330. /* Indicate the raw buffer which is the original string passed as an
  331. argument of regexec(), re_search(), etc.. */
  332. const unsigned char *raw_mbs;
  333. /* Store the multibyte string. In case of "case insensitive mode" like
  334. REG_ICASE, upper cases of the string are stored, otherwise MBS points
  335. the same address that RAW_MBS points. */
  336. unsigned char *mbs;
  337. #ifdef RE_ENABLE_I18N
  338. /* Store the wide character string which is corresponding to MBS. */
  339. wint_t *wcs;
  340. int *offsets;
  341. mbstate_t cur_state;
  342. #endif
  343. /* Index in RAW_MBS. Each character mbs[i] corresponds to
  344. raw_mbs[raw_mbs_idx + i]. */
  345. int raw_mbs_idx;
  346. /* The length of the valid characters in the buffers. */
  347. int valid_len;
  348. /* The corresponding number of bytes in raw_mbs array. */
  349. int valid_raw_len;
  350. /* The length of the buffers MBS and WCS. */
  351. int bufs_len;
  352. /* The index in MBS, which is updated by re_string_fetch_byte. */
  353. int cur_idx;
  354. /* length of RAW_MBS array. */
  355. int raw_len;
  356. /* This is RAW_LEN - RAW_MBS_IDX + VALID_LEN - VALID_RAW_LEN. */
  357. int len;
  358. /* End of the buffer may be shorter than its length in the cases such
  359. as re_match_2, re_search_2. Then, we use STOP for end of the buffer
  360. instead of LEN. */
  361. int raw_stop;
  362. /* This is RAW_STOP - RAW_MBS_IDX adjusted through OFFSETS. */
  363. int stop;
  364. /* The context of mbs[0]. We store the context independently, since
  365. the context of mbs[0] may be different from raw_mbs[0], which is
  366. the beginning of the input string. */
  367. unsigned int tip_context;
  368. /* The translation passed as a part of an argument of re_compile_pattern. */
  369. RE_TRANSLATE_TYPE trans;
  370. /* Copy of re_dfa_t's word_char. */
  371. re_const_bitset_ptr_t word_char;
  372. /* 1 if REG_ICASE. */
  373. unsigned char icase;
  374. unsigned char is_utf8;
  375. unsigned char map_notascii;
  376. unsigned char mbs_allocated;
  377. unsigned char offsets_needed;
  378. unsigned char newline_anchor;
  379. unsigned char word_ops_used;
  380. int mb_cur_max;
  381. };
  382. typedef struct re_string_t re_string_t;
  383. struct re_dfa_t;
  384. typedef struct re_dfa_t re_dfa_t;
  385. #ifndef _LIBC
  386. # ifdef __i386__
  387. # define internal_function __attribute ((regparm (3), stdcall))
  388. # else
  389. # define internal_function
  390. # endif
  391. #endif
  392. #ifndef NOT_IN_libc
  393. static reg_errcode_t re_string_realloc_buffers (re_string_t *pstr,
  394. int new_buf_len)
  395. internal_function;
  396. # ifdef RE_ENABLE_I18N
  397. static void build_wcs_buffer (re_string_t *pstr) internal_function;
  398. static int build_wcs_upper_buffer (re_string_t *pstr) 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, int idx,
  403. int eflags)
  404. internal_function __attribute ((pure));
  405. #endif
  406. #define re_string_peek_byte(pstr, offset) \
  407. ((pstr)->mbs[(pstr)->cur_idx + offset])
  408. #define re_string_fetch_byte(pstr) \
  409. ((pstr)->mbs[(pstr)->cur_idx++])
  410. #define re_string_first_byte(pstr, idx) \
  411. ((idx) == (pstr)->valid_len || (pstr)->wcs[idx] != WEOF)
  412. #define re_string_is_single_byte_char(pstr, idx) \
  413. ((pstr)->wcs[idx] != WEOF && ((pstr)->valid_len == (idx) + 1 \
  414. || (pstr)->wcs[(idx) + 1] != WEOF))
  415. #define re_string_eoi(pstr) ((pstr)->stop <= (pstr)->cur_idx)
  416. #define re_string_cur_idx(pstr) ((pstr)->cur_idx)
  417. #define re_string_get_buffer(pstr) ((pstr)->mbs)
  418. #define re_string_length(pstr) ((pstr)->len)
  419. #define re_string_byte_at(pstr,idx) ((pstr)->mbs[idx])
  420. #define re_string_skip_bytes(pstr,idx) ((pstr)->cur_idx += (idx))
  421. #define re_string_set_index(pstr,idx) ((pstr)->cur_idx = (idx))
  422. //#include <alloca.h>
  423. #ifndef _LIBC
  424. # if HAVE_ALLOCA
  425. /* The OS usually guarantees only one guard page at the bottom of the stack,
  426. and a page size can be as small as 4096 bytes. So we cannot safely
  427. allocate anything larger than 4096 bytes. Also care for the possibility
  428. of a few compiler-allocated temporary stack slots. */
  429. # define __libc_use_alloca(n) ((n) < 4032)
  430. # else
  431. /* alloca is implemented with malloc, so just use malloc. */
  432. # define __libc_use_alloca(n) 0
  433. # endif
  434. #endif
  435. #define re_malloc(t,n) ((t *) malloc ((n) * sizeof (t)))
  436. #define re_realloc(p,t,n) ((t *) realloc (p, (n) * sizeof (t)))
  437. #define re_free(p) free (p)
  438. struct bin_tree_t
  439. {
  440. struct bin_tree_t *parent;
  441. struct bin_tree_t *left;
  442. struct bin_tree_t *right;
  443. struct bin_tree_t *first;
  444. struct bin_tree_t *next;
  445. re_token_t token;
  446. /* `node_idx' is the index in dfa->nodes, if `type' == 0.
  447. Otherwise `type' indicate the type of this node. */
  448. int node_idx;
  449. };
  450. typedef struct bin_tree_t bin_tree_t;
  451. #define BIN_TREE_STORAGE_SIZE \
  452. ((1024 - sizeof (void *)) / sizeof (bin_tree_t))
  453. struct bin_tree_storage_t
  454. {
  455. struct bin_tree_storage_t *next;
  456. bin_tree_t data[BIN_TREE_STORAGE_SIZE];
  457. };
  458. typedef struct bin_tree_storage_t bin_tree_storage_t;
  459. #define CONTEXT_WORD 1
  460. #define CONTEXT_NEWLINE (CONTEXT_WORD << 1)
  461. #define CONTEXT_BEGBUF (CONTEXT_NEWLINE << 1)
  462. #define CONTEXT_ENDBUF (CONTEXT_BEGBUF << 1)
  463. #define IS_WORD_CONTEXT(c) ((c) & CONTEXT_WORD)
  464. #define IS_NEWLINE_CONTEXT(c) ((c) & CONTEXT_NEWLINE)
  465. #define IS_BEGBUF_CONTEXT(c) ((c) & CONTEXT_BEGBUF)
  466. #define IS_ENDBUF_CONTEXT(c) ((c) & CONTEXT_ENDBUF)
  467. #define IS_ORDINARY_CONTEXT(c) ((c) == 0)
  468. #define IS_WORD_CHAR(ch) (isalnum (ch) || (ch) == '_')
  469. #define IS_NEWLINE(ch) ((ch) == NEWLINE_CHAR)
  470. #define IS_WIDE_WORD_CHAR(ch) (iswalnum (ch) || (ch) == L'_')
  471. #define IS_WIDE_NEWLINE(ch) ((ch) == WIDE_NEWLINE_CHAR)
  472. #define NOT_SATISFY_PREV_CONSTRAINT(constraint,context) \
  473. ((((constraint) & PREV_WORD_CONSTRAINT) && !IS_WORD_CONTEXT (context)) \
  474. || ((constraint & PREV_NOTWORD_CONSTRAINT) && IS_WORD_CONTEXT (context)) \
  475. || ((constraint & PREV_NEWLINE_CONSTRAINT) && !IS_NEWLINE_CONTEXT (context))\
  476. || ((constraint & PREV_BEGBUF_CONSTRAINT) && !IS_BEGBUF_CONTEXT (context)))
  477. #define NOT_SATISFY_NEXT_CONSTRAINT(constraint,context) \
  478. ((((constraint) & NEXT_WORD_CONSTRAINT) && !IS_WORD_CONTEXT (context)) \
  479. || (((constraint) & NEXT_NOTWORD_CONSTRAINT) && IS_WORD_CONTEXT (context)) \
  480. || (((constraint) & NEXT_NEWLINE_CONSTRAINT) && !IS_NEWLINE_CONTEXT (context)) \
  481. || (((constraint) & NEXT_ENDBUF_CONSTRAINT) && !IS_ENDBUF_CONTEXT (context)))
  482. struct re_dfastate_t
  483. {
  484. unsigned int hash;
  485. re_node_set nodes;
  486. re_node_set non_eps_nodes;
  487. re_node_set inveclosure;
  488. re_node_set *entrance_nodes;
  489. struct re_dfastate_t **trtable, **word_trtable;
  490. unsigned int context : 4;
  491. unsigned int halt : 1;
  492. /* If this state can accept `multi byte'.
  493. Note that we refer to multibyte characters, and multi character
  494. collating elements as `multi byte'. */
  495. unsigned int accept_mb : 1;
  496. /* If this state has backreference node(s). */
  497. unsigned int has_backref : 1;
  498. unsigned int has_constraint : 1;
  499. };
  500. typedef struct re_dfastate_t re_dfastate_t;
  501. struct re_state_table_entry
  502. {
  503. int num;
  504. int alloc;
  505. re_dfastate_t **array;
  506. };
  507. /* Array type used in re_sub_match_last_t and re_sub_match_top_t. */
  508. typedef struct
  509. {
  510. int next_idx;
  511. int alloc;
  512. re_dfastate_t **array;
  513. } state_array_t;
  514. /* Store information about the node NODE whose type is OP_CLOSE_SUBEXP. */
  515. typedef struct
  516. {
  517. int node;
  518. int str_idx; /* The position NODE match at. */
  519. state_array_t path;
  520. } re_sub_match_last_t;
  521. /* Store information about the node NODE whose type is OP_OPEN_SUBEXP.
  522. And information about the node, whose type is OP_CLOSE_SUBEXP,
  523. corresponding to NODE is stored in LASTS. */
  524. typedef struct
  525. {
  526. int str_idx;
  527. int node;
  528. state_array_t *path;
  529. int alasts; /* Allocation size of LASTS. */
  530. int nlasts; /* The number of LASTS. */
  531. re_sub_match_last_t **lasts;
  532. } re_sub_match_top_t;
  533. struct re_backref_cache_entry
  534. {
  535. int node;
  536. int str_idx;
  537. int subexp_from;
  538. int subexp_to;
  539. char more;
  540. char unused;
  541. unsigned short int eps_reachable_subexps_map;
  542. };
  543. typedef struct
  544. {
  545. /* The string object corresponding to the input string. */
  546. re_string_t input;
  547. #if defined _LIBC || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L)
  548. const re_dfa_t *const dfa;
  549. #else
  550. const re_dfa_t *dfa;
  551. #endif
  552. /* EFLAGS of the argument of regexec. */
  553. int eflags;
  554. /* Where the matching ends. */
  555. int match_last;
  556. int last_node;
  557. /* The state log used by the matcher. */
  558. re_dfastate_t **state_log;
  559. int state_log_top;
  560. /* Back reference cache. */
  561. int nbkref_ents;
  562. int abkref_ents;
  563. struct re_backref_cache_entry *bkref_ents;
  564. int max_mb_elem_len;
  565. int nsub_tops;
  566. int asub_tops;
  567. re_sub_match_top_t **sub_tops;
  568. } re_match_context_t;
  569. typedef struct
  570. {
  571. re_dfastate_t **sifted_states;
  572. re_dfastate_t **limited_states;
  573. int last_node;
  574. int last_str_idx;
  575. re_node_set limits;
  576. } re_sift_context_t;
  577. struct re_fail_stack_ent_t
  578. {
  579. int idx;
  580. int node;
  581. regmatch_t *regs;
  582. re_node_set eps_via_nodes;
  583. };
  584. struct re_fail_stack_t
  585. {
  586. int num;
  587. int alloc;
  588. struct re_fail_stack_ent_t *stack;
  589. };
  590. struct re_dfa_t
  591. {
  592. re_token_t *nodes;
  593. size_t nodes_alloc;
  594. size_t nodes_len;
  595. int *nexts;
  596. int *org_indices;
  597. re_node_set *edests;
  598. re_node_set *eclosures;
  599. re_node_set *inveclosures;
  600. struct re_state_table_entry *state_table;
  601. re_dfastate_t *init_state;
  602. re_dfastate_t *init_state_word;
  603. re_dfastate_t *init_state_nl;
  604. re_dfastate_t *init_state_begbuf;
  605. bin_tree_t *str_tree;
  606. bin_tree_storage_t *str_tree_storage;
  607. re_bitset_ptr_t sb_char;
  608. int str_tree_storage_idx;
  609. /* number of subexpressions `re_nsub' is in regex_t. */
  610. unsigned int state_hash_mask;
  611. int init_node;
  612. int nbackref; /* The number of backreference in this dfa. */
  613. /* Bitmap expressing which backreference is used. */
  614. bitset_word_t used_bkref_map;
  615. bitset_word_t completed_bkref_map;
  616. unsigned int has_plural_match : 1;
  617. /* If this dfa has "multibyte node", which is a backreference or
  618. a node which can accept multibyte character or multi character
  619. collating element. */
  620. unsigned int has_mb_node : 1;
  621. unsigned int is_utf8 : 1;
  622. unsigned int map_notascii : 1;
  623. unsigned int word_ops_used : 1;
  624. int mb_cur_max;
  625. bitset_t word_char;
  626. reg_syntax_t syntax;
  627. int *subexp_map;
  628. #ifdef DEBUG
  629. char* re_str;
  630. #endif
  631. __libc_lock_define (, lock)
  632. };
  633. #define re_node_set_init_empty(set) memset (set, '\0', sizeof (re_node_set))
  634. #define re_node_set_remove(set,id) \
  635. (re_node_set_remove_at (set, re_node_set_contains (set, id) - 1))
  636. #define re_node_set_empty(p) ((p)->nelem = 0)
  637. #define re_node_set_free(set) re_free ((set)->elems)
  638. typedef enum
  639. {
  640. SB_CHAR,
  641. MB_CHAR,
  642. EQUIV_CLASS,
  643. COLL_SYM,
  644. CHAR_CLASS
  645. } bracket_elem_type;
  646. typedef struct
  647. {
  648. bracket_elem_type type;
  649. union
  650. {
  651. unsigned char ch;
  652. unsigned char *name;
  653. wchar_t wch;
  654. } opr;
  655. } bracket_elem_t;
  656. /* Inline functions for bitset operation. */
  657. static inline void
  658. bitset_not (bitset_t set)
  659. {
  660. int bitset_i;
  661. for (bitset_i = 0; bitset_i < BITSET_WORDS; ++bitset_i)
  662. set[bitset_i] = ~set[bitset_i];
  663. }
  664. static inline void
  665. bitset_merge (bitset_t dest, const bitset_t src)
  666. {
  667. int bitset_i;
  668. for (bitset_i = 0; bitset_i < BITSET_WORDS; ++bitset_i)
  669. dest[bitset_i] |= src[bitset_i];
  670. }
  671. static inline void
  672. bitset_mask (bitset_t dest, const bitset_t src)
  673. {
  674. int bitset_i;
  675. for (bitset_i = 0; bitset_i < BITSET_WORDS; ++bitset_i)
  676. dest[bitset_i] &= src[bitset_i];
  677. }
  678. #ifdef RE_ENABLE_I18N
  679. /* Inline functions for re_string. */
  680. static inline int
  681. internal_function __attribute ((pure))
  682. re_string_char_size_at (const re_string_t *pstr, int idx)
  683. {
  684. int byte_idx;
  685. if (pstr->mb_cur_max == 1)
  686. return 1;
  687. for (byte_idx = 1; idx + byte_idx < pstr->valid_len; ++byte_idx)
  688. if (pstr->wcs[idx + byte_idx] != WEOF)
  689. break;
  690. return byte_idx;
  691. }
  692. static inline wint_t
  693. internal_function __attribute ((pure))
  694. re_string_wchar_at (const re_string_t *pstr, int idx)
  695. {
  696. if (pstr->mb_cur_max == 1)
  697. return (wint_t) pstr->mbs[idx];
  698. return (wint_t) pstr->wcs[idx];
  699. }
  700. # ifndef NOT_IN_libc
  701. static int
  702. internal_function __attribute ((pure))
  703. re_string_elem_size_at (const re_string_t *pstr, int idx)
  704. {
  705. # ifdef _LIBC
  706. const unsigned char *p, *extra;
  707. const int32_t *table, *indirect;
  708. int32_t tmp;
  709. # include <locale/weight.h>
  710. uint_fast32_t nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
  711. if (nrules != 0)
  712. {
  713. table = (const int32_t *) _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB);
  714. extra = (const unsigned char *)
  715. _NL_CURRENT (LC_COLLATE, _NL_COLLATE_EXTRAMB);
  716. indirect = (const int32_t *) _NL_CURRENT (LC_COLLATE,
  717. _NL_COLLATE_INDIRECTMB);
  718. p = pstr->mbs + idx;
  719. tmp = findidx (&p);
  720. return p - pstr->mbs - idx;
  721. }
  722. else
  723. # endif /* _LIBC */
  724. return 1;
  725. }
  726. # endif
  727. #endif /* RE_ENABLE_I18N */
  728. #endif /* _REGEX_INTERNAL_H */
  729. /* Extended regular expression matching and search library.
  730. Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
  731. This file is part of the GNU C Library.
  732. Contributed by Isamu Hasegawa <isamu@yamato.ibm.com>.
  733. The GNU C Library is free software; you can redistribute it and/or
  734. modify it under the terms of the GNU Lesser General Public
  735. License as published by the Free Software Foundation; either
  736. version 2.1 of the License, or (at your option) any later version.
  737. The GNU C Library is distributed in the hope that it will be useful,
  738. but WITHOUT ANY WARRANTY; without even the implied warranty of
  739. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  740. Lesser General Public License for more details.
  741. You should have received a copy of the GNU Lesser General Public
  742. License along with the GNU C Library; if not, write to the Free
  743. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  744. 02111-1307 USA. */
  745. static void re_string_construct_common (const char *str, int len,
  746. re_string_t *pstr,
  747. RE_TRANSLATE_TYPE trans, int icase,
  748. const re_dfa_t *dfa) internal_function;
  749. static re_dfastate_t *create_ci_newstate (const re_dfa_t *dfa,
  750. const re_node_set *nodes,
  751. unsigned int hash) internal_function;
  752. static re_dfastate_t *create_cd_newstate (const re_dfa_t *dfa,
  753. const re_node_set *nodes,
  754. unsigned int context,
  755. unsigned int hash) internal_function;
  756. /* Functions for string operation. */
  757. /* This function allocate the buffers. It is necessary to call
  758. re_string_reconstruct before using the object. */
  759. static reg_errcode_t
  760. internal_function
  761. re_string_allocate (re_string_t *pstr, const char *str, int len, int init_len,
  762. RE_TRANSLATE_TYPE trans, int icase, const re_dfa_t *dfa)
  763. {
  764. reg_errcode_t ret;
  765. int init_buf_len;
  766. /* Ensure at least one character fits into the buffers. */
  767. if (init_len < dfa->mb_cur_max)
  768. init_len = dfa->mb_cur_max;
  769. init_buf_len = (len + 1 < init_len) ? len + 1: init_len;
  770. re_string_construct_common (str, len, pstr, trans, icase, dfa);
  771. ret = re_string_realloc_buffers (pstr, init_buf_len);
  772. if (BE (ret != REG_NOERROR, 0))
  773. return ret;
  774. pstr->word_char = dfa->word_char;
  775. pstr->word_ops_used = dfa->word_ops_used;
  776. pstr->mbs = pstr->mbs_allocated ? pstr->mbs : (unsigned char *) str;
  777. pstr->valid_len = (pstr->mbs_allocated || dfa->mb_cur_max > 1) ? 0 : len;
  778. pstr->valid_raw_len = pstr->valid_len;
  779. return REG_NOERROR;
  780. }
  781. /* This function allocate the buffers, and initialize them. */
  782. static reg_errcode_t
  783. internal_function
  784. re_string_construct (re_string_t *pstr, const char *str, int len,
  785. RE_TRANSLATE_TYPE trans, int icase, const re_dfa_t *dfa)
  786. {
  787. reg_errcode_t ret;
  788. memset (pstr, '\0', sizeof (re_string_t));
  789. re_string_construct_common (str, len, pstr, trans, icase, dfa);
  790. if (len > 0)
  791. {
  792. ret = re_string_realloc_buffers (pstr, len + 1);
  793. if (BE (ret != REG_NOERROR, 0))
  794. return ret;
  795. }
  796. pstr->mbs = pstr->mbs_allocated ? pstr->mbs : (unsigned char *) str;
  797. if (icase)
  798. {
  799. #ifdef RE_ENABLE_I18N
  800. if (dfa->mb_cur_max > 1)
  801. {
  802. while (1)
  803. {
  804. ret = build_wcs_upper_buffer (pstr);
  805. if (BE (ret != REG_NOERROR, 0))
  806. return ret;
  807. if (pstr->valid_raw_len >= len)
  808. break;
  809. if (pstr->bufs_len > pstr->valid_len + dfa->mb_cur_max)
  810. break;
  811. ret = re_string_realloc_buffers (pstr, pstr->bufs_len * 2);
  812. if (BE (ret != REG_NOERROR, 0))
  813. return ret;
  814. }
  815. }
  816. else
  817. #endif /* RE_ENABLE_I18N */
  818. build_upper_buffer (pstr);
  819. }
  820. else
  821. {
  822. #ifdef RE_ENABLE_I18N
  823. if (dfa->mb_cur_max > 1)
  824. build_wcs_buffer (pstr);
  825. else
  826. #endif /* RE_ENABLE_I18N */
  827. {
  828. if (trans != NULL)
  829. re_string_translate_buffer (pstr);
  830. else
  831. {
  832. pstr->valid_len = pstr->bufs_len;
  833. pstr->valid_raw_len = pstr->bufs_len;
  834. }
  835. }
  836. }
  837. return REG_NOERROR;
  838. }
  839. /* Helper functions for re_string_allocate, and re_string_construct. */
  840. static reg_errcode_t
  841. internal_function
  842. re_string_realloc_buffers (re_string_t *pstr, int new_buf_len)
  843. {
  844. #ifdef RE_ENABLE_I18N
  845. if (pstr->mb_cur_max > 1)
  846. {
  847. wint_t *new_wcs = re_realloc (pstr->wcs, wint_t, new_buf_len);
  848. if (BE (new_wcs == NULL, 0))
  849. return REG_ESPACE;
  850. pstr->wcs = new_wcs;
  851. if (pstr->offsets != NULL)
  852. {
  853. int *new_offsets = re_realloc (pstr->offsets, int, new_buf_len);
  854. if (BE (new_offsets == NULL, 0))
  855. return REG_ESPACE;
  856. pstr->offsets = new_offsets;
  857. }
  858. }
  859. #endif /* RE_ENABLE_I18N */
  860. if (pstr->mbs_allocated)
  861. {
  862. unsigned char *new_mbs = re_realloc (pstr->mbs, unsigned char,
  863. new_buf_len);
  864. if (BE (new_mbs == NULL, 0))
  865. return REG_ESPACE;
  866. pstr->mbs = new_mbs;
  867. }
  868. pstr->bufs_len = new_buf_len;
  869. return REG_NOERROR;
  870. }
  871. static void
  872. internal_function
  873. re_string_construct_common (const char *str, int len, re_string_t *pstr,
  874. RE_TRANSLATE_TYPE trans, int icase,
  875. const re_dfa_t *dfa)
  876. {
  877. pstr->raw_mbs = (const unsigned char *) str;
  878. pstr->len = len;
  879. pstr->raw_len = len;
  880. pstr->trans = trans;
  881. pstr->icase = icase ? 1 : 0;
  882. pstr->mbs_allocated = (trans != NULL || icase);
  883. pstr->mb_cur_max = dfa->mb_cur_max;
  884. pstr->is_utf8 = dfa->is_utf8;
  885. pstr->map_notascii = dfa->map_notascii;
  886. pstr->stop = pstr->len;
  887. pstr->raw_stop = pstr->stop;
  888. }
  889. #ifdef RE_ENABLE_I18N
  890. /* Build wide character buffer PSTR->WCS.
  891. If the byte sequence of the string are:
  892. <mb1>(0), <mb1>(1), <mb2>(0), <mb2>(1), <sb3>
  893. Then wide character buffer will be:
  894. <wc1> , WEOF , <wc2> , WEOF , <wc3>
  895. We use WEOF for padding, they indicate that the position isn't
  896. a first byte of a multibyte character.
  897. Note that this function assumes PSTR->VALID_LEN elements are already
  898. built and starts from PSTR->VALID_LEN. */
  899. static void
  900. internal_function
  901. build_wcs_buffer (re_string_t *pstr)
  902. {
  903. #ifdef _LIBC
  904. unsigned char buf[MB_LEN_MAX];
  905. assert (MB_LEN_MAX >= pstr->mb_cur_max);
  906. #else
  907. unsigned char buf[64];
  908. #endif
  909. mbstate_t prev_st;
  910. int byte_idx, end_idx, remain_len;
  911. size_t mbclen;
  912. /* Build the buffers from pstr->valid_len to either pstr->len or
  913. pstr->bufs_len. */
  914. end_idx = (pstr->bufs_len > pstr->len) ? pstr->len : pstr->bufs_len;
  915. for (byte_idx = pstr->valid_len; byte_idx < end_idx;)
  916. {
  917. wchar_t wc;
  918. const char *p;
  919. remain_len = end_idx - byte_idx;
  920. prev_st = pstr->cur_state;
  921. /* Apply the translation if we need. */
  922. if (BE (pstr->trans != NULL, 0))
  923. {
  924. int i, ch;
  925. for (i = 0; i < pstr->mb_cur_max && i < remain_len; ++i)
  926. {
  927. ch = pstr->raw_mbs [pstr->raw_mbs_idx + byte_idx + i];
  928. buf[i] = pstr->mbs[byte_idx + i] = pstr->trans[ch];
  929. }
  930. p = (const char *) buf;
  931. }
  932. else
  933. p = (const char *) pstr->raw_mbs + pstr->raw_mbs_idx + byte_idx;
  934. mbclen = mbrtowc (&wc, p, remain_len, &pstr->cur_state);
  935. if (BE (mbclen == (size_t) -2, 0))
  936. {
  937. /* The buffer doesn't have enough space, finish to build. */
  938. pstr->cur_state = prev_st;
  939. break;
  940. }
  941. else if (BE (mbclen == (size_t) -1 || mbclen == 0, 0))
  942. {
  943. /* We treat these cases as a singlebyte character. */
  944. mbclen = 1;
  945. wc = (wchar_t) pstr->raw_mbs[pstr->raw_mbs_idx + byte_idx];
  946. if (BE (pstr->trans != NULL, 0))
  947. wc = pstr->trans[wc];
  948. pstr->cur_state = prev_st;
  949. }
  950. /* Write wide character and padding. */
  951. pstr->wcs[byte_idx++] = wc;
  952. /* Write paddings. */
  953. for (remain_len = byte_idx + mbclen - 1; byte_idx < remain_len ;)
  954. pstr->wcs[byte_idx++] = WEOF;
  955. }
  956. pstr->valid_len = byte_idx;
  957. pstr->valid_raw_len = byte_idx;
  958. }
  959. /* Build wide character buffer PSTR->WCS like build_wcs_buffer,
  960. but for REG_ICASE. */
  961. static reg_errcode_t
  962. internal_function
  963. build_wcs_upper_buffer (re_string_t *pstr)
  964. {
  965. mbstate_t prev_st;
  966. int src_idx, byte_idx, end_idx, remain_len;
  967. size_t mbclen;
  968. #ifdef _LIBC
  969. char buf[MB_LEN_MAX];
  970. assert (MB_LEN_MAX >= pstr->mb_cur_max);
  971. #else
  972. char buf[64];
  973. #endif
  974. byte_idx = pstr->valid_len;
  975. end_idx = (pstr->bufs_len > pstr->len) ? pstr->len : pstr->bufs_len;
  976. /* The following optimization assumes that ASCII characters can be
  977. mapped to wide characters with a simple cast. */
  978. if (! pstr->map_notascii && pstr->trans == NULL && !pstr->offsets_needed)
  979. {
  980. while (byte_idx < end_idx)
  981. {
  982. wchar_t wc;
  983. if (isascii (pstr->raw_mbs[pstr->raw_mbs_idx + byte_idx])
  984. && mbsinit (&pstr->cur_state))
  985. {
  986. /* In case of a singlebyte character. */
  987. pstr->mbs[byte_idx]
  988. = toupper (pstr->raw_mbs[pstr->raw_mbs_idx + byte_idx]);
  989. /* The next step uses the assumption that wchar_t is encoded
  990. ASCII-safe: all ASCII values can be converted like this. */
  991. pstr->wcs[byte_idx] = (wchar_t) pstr->mbs[byte_idx];
  992. ++byte_idx;
  993. continue;
  994. }
  995. remain_len = end_idx - byte_idx;
  996. prev_st = pstr->cur_state;
  997. mbclen = mbrtowc (&wc,
  998. ((const char *) pstr->raw_mbs + pstr->raw_mbs_idx
  999. + byte_idx), remain_len, &pstr->cur_state);
  1000. if (BE (mbclen + 2 > 2, 1))
  1001. {
  1002. wchar_t wcu = wc;
  1003. if (iswlower (wc))
  1004. {
  1005. size_t mbcdlen;
  1006. wcu = towupper (wc);
  1007. mbcdlen = wcrtomb (buf, wcu, &prev_st);
  1008. if (BE (mbclen == mbcdlen, 1))
  1009. memcpy (pstr->mbs + byte_idx, buf, mbclen);
  1010. else
  1011. {
  1012. src_idx = byte_idx;
  1013. goto offsets_needed;
  1014. }
  1015. }
  1016. else
  1017. memcpy (pstr->mbs + byte_idx,
  1018. pstr->raw_mbs + pstr->raw_mbs_idx + byte_idx, mbclen);
  1019. pstr->wcs[byte_idx++] = wcu;
  1020. /* Write paddings. */
  1021. for (remain_len = byte_idx + mbclen - 1; byte_idx < remain_len ;)
  1022. pstr->wcs[byte_idx++] = WEOF;
  1023. }
  1024. else if (mbclen == (size_t) -1 || mbclen == 0)
  1025. {
  1026. /* It is an invalid character or '\0'. Just use the byte. */
  1027. int ch = pstr->raw_mbs[pstr->raw_mbs_idx + byte_idx];
  1028. pstr->mbs[byte_idx] = ch;
  1029. /* And also cast it to wide char. */
  1030. pstr->wcs[byte_idx++] = (wchar_t) ch;
  1031. if (BE (mbclen == (size_t) -1, 0))
  1032. pstr->cur_state = prev_st;
  1033. }
  1034. else
  1035. {
  1036. /* The buffer doesn't have enough space, finish to build. */
  1037. pstr->cur_state = prev_st;
  1038. break;
  1039. }
  1040. }
  1041. pstr->valid_len = byte_idx;
  1042. pstr->valid_raw_len = byte_idx;
  1043. return REG_NOERROR;
  1044. }
  1045. else
  1046. for (src_idx = pstr->valid_raw_len; byte_idx < end_idx;)
  1047. {
  1048. wchar_t wc;
  1049. const char *p;
  1050. offsets_needed:
  1051. remain_len = end_idx - byte_idx;
  1052. prev_st = pstr->cur_state;
  1053. if (BE (pstr->trans != NULL, 0))
  1054. {
  1055. int i, ch;
  1056. for (i = 0; i < pstr->mb_cur_max && i < remain_len; ++i)
  1057. {
  1058. ch = pstr->raw_mbs [pstr->raw_mbs_idx + src_idx + i];
  1059. buf[i] = pstr->trans[ch];
  1060. }
  1061. p = (const char *) buf;
  1062. }
  1063. else
  1064. p = (const char *) pstr->raw_mbs + pstr->raw_mbs_idx + src_idx;
  1065. mbclen = mbrtowc (&wc, p, remain_len, &pstr->cur_state);
  1066. if (BE (mbclen + 2 > 2, 1))
  1067. {
  1068. wchar_t wcu = wc;
  1069. if (iswlower (wc))
  1070. {
  1071. size_t mbcdlen;
  1072. wcu = towupper (wc);
  1073. mbcdlen = wcrtomb ((char *) buf, wcu, &prev_st);
  1074. if (BE (mbclen == mbcdlen, 1))
  1075. memcpy (pstr->mbs + byte_idx, buf, mbclen);
  1076. else if (mbcdlen != (size_t) -1)
  1077. {
  1078. size_t i;
  1079. if (byte_idx + mbcdlen > pstr->bufs_len)
  1080. {
  1081. pstr->cur_state = prev_st;
  1082. break;
  1083. }
  1084. if (pstr->offsets == NULL)
  1085. {
  1086. pstr->offsets = re_malloc (int, pstr->bufs_len);
  1087. if (pstr->offsets == NULL)
  1088. return REG_ESPACE;
  1089. }
  1090. if (!pstr->offsets_needed)
  1091. {
  1092. for (i = 0; i < (size_t) byte_idx; ++i)
  1093. pstr->offsets[i] = i;
  1094. pstr->offsets_needed = 1;
  1095. }
  1096. memcpy (pstr->mbs + byte_idx, buf, mbcdlen);
  1097. pstr->wcs[byte_idx] = wcu;
  1098. pstr->offsets[byte_idx] = src_idx;
  1099. for (i = 1; i < mbcdlen; ++i)
  1100. {
  1101. pstr->offsets[byte_idx + i]
  1102. = src_idx + (i < mbclen ? i : mbclen - 1);
  1103. pstr->wcs[byte_idx + i] = WEOF;
  1104. }
  1105. pstr->len += mbcdlen - mbclen;
  1106. if (pstr->raw_stop > src_idx)
  1107. pstr->stop += mbcdlen - mbclen;
  1108. end_idx = (pstr->bufs_len > pstr->len)
  1109. ? pstr->len : pstr->bufs_len;
  1110. byte_idx += mbcdlen;
  1111. src_idx += mbclen;
  1112. continue;
  1113. }
  1114. else
  1115. memcpy (pstr->mbs + byte_idx, p, mbclen);
  1116. }
  1117. else
  1118. memcpy (pstr->mbs + byte_idx, p, mbclen);
  1119. if (BE (pstr->offsets_needed != 0, 0))
  1120. {
  1121. size_t i;
  1122. for (i = 0; i < mbclen; ++i)
  1123. pstr->offsets[byte_idx + i] = src_idx + i;
  1124. }
  1125. src_idx += mbclen;
  1126. pstr->wcs[byte_idx++] = wcu;
  1127. /* Write paddings. */
  1128. for (remain_len = byte_idx + mbclen - 1; byte_idx < remain_len ;)
  1129. pstr->wcs[byte_idx++] = WEOF;
  1130. }
  1131. else if (mbclen == (size_t) -1 || mbclen == 0)
  1132. {
  1133. /* It is an invalid character or '\0'. Just use the byte. */
  1134. int ch = pstr->raw_mbs[pstr->raw_mbs_idx + src_idx];
  1135. if (BE (pstr->trans != NULL, 0))
  1136. ch = pstr->trans [ch];
  1137. pstr->mbs[byte_idx] = ch;
  1138. if (BE (pstr->offsets_needed != 0, 0))
  1139. pstr->offsets[byte_idx] = src_idx;
  1140. ++src_idx;
  1141. /* And also cast it to wide char. */
  1142. pstr->wcs[byte_idx++] = (wchar_t) ch;
  1143. if (BE (mbclen == (size_t) -1, 0))
  1144. pstr->cur_state = prev_st;
  1145. }
  1146. else
  1147. {
  1148. /* The buffer doesn't have enough space, finish to build. */
  1149. pstr->cur_state = prev_st;
  1150. break;
  1151. }
  1152. }
  1153. pstr->valid_len = byte_idx;
  1154. pstr->valid_raw_len = src_idx;
  1155. return REG_NOERROR;
  1156. }
  1157. /* Skip characters until the index becomes greater than NEW_RAW_IDX.
  1158. Return the index. */
  1159. static int
  1160. internal_function
  1161. re_string_skip_chars (re_string_t *pstr, int new_raw_idx, wint_t *last_wc)
  1162. {
  1163. mbstate_t prev_st;
  1164. int rawbuf_idx;
  1165. size_t mbclen;
  1166. wchar_t wc = WEOF;
  1167. /* Skip the characters which are not necessary to check. */
  1168. for (rawbuf_idx = pstr->raw_mbs_idx + pstr->valid_raw_len;
  1169. rawbuf_idx < new_raw_idx;)
  1170. {
  1171. int remain_len;
  1172. remain_len = pstr->len - rawbuf_idx;
  1173. prev_st = pstr->cur_state;
  1174. mbclen = mbrtowc (&wc, (const char *) pstr->raw_mbs + rawbuf_idx,
  1175. remain_len, &pstr->cur_state);
  1176. if (BE (mbclen == (size_t) -2 || mbclen == (size_t) -1 || mbclen == 0, 0))
  1177. {
  1178. /* We treat these cases as a single byte character. */
  1179. if (mbclen == 0 || remain_len == 0)
  1180. wc = L'\0';
  1181. else
  1182. wc = *(unsigned char *) (pstr->raw_mbs + rawbuf_idx);
  1183. mbclen = 1;
  1184. pstr->cur_state = prev_st;
  1185. }
  1186. /* Then proceed the next character. */
  1187. rawbuf_idx += mbclen;
  1188. }
  1189. *last_wc = (wint_t) wc;
  1190. return rawbuf_idx;
  1191. }
  1192. #endif /* RE_ENABLE_I18N */
  1193. /* Build the buffer PSTR->MBS, and apply the translation if we need.
  1194. This function is used in case of REG_ICASE. */
  1195. static void
  1196. internal_function
  1197. build_upper_buffer (re_string_t *pstr)
  1198. {
  1199. int char_idx, end_idx;
  1200. end_idx = (pstr->bufs_len > pstr->len) ? pstr->len : pstr->bufs_len;
  1201. for (char_idx = pstr->valid_len; char_idx < end_idx; ++char_idx)
  1202. {
  1203. int ch = pstr->raw_mbs[pstr->raw_mbs_idx + char_idx];
  1204. if (BE (pstr->trans != NULL, 0))
  1205. ch = pstr->trans[ch];
  1206. if (islower (ch))
  1207. pstr->mbs[char_idx] = toupper (ch);
  1208. else
  1209. pstr->mbs[char_idx] = ch;
  1210. }
  1211. pstr->valid_len = char_idx;
  1212. pstr->valid_raw_len = char_idx;
  1213. }
  1214. /* Apply TRANS to the buffer in PSTR. */
  1215. static void
  1216. internal_function
  1217. re_string_translate_buffer (re_string_t *pstr)
  1218. {
  1219. int buf_idx, end_idx;
  1220. end_idx = (pstr->bufs_len > pstr->len) ? pstr->len : pstr->bufs_len;
  1221. for (buf_idx = pstr->valid_len; buf_idx < end_idx; ++buf_idx)
  1222. {
  1223. int ch = pstr->raw_mbs[pstr->raw_mbs_idx + buf_idx];
  1224. pstr->mbs[buf_idx] = pstr->trans[ch];
  1225. }
  1226. pstr->valid_len = buf_idx;
  1227. pstr->valid_raw_len = buf_idx;
  1228. }
  1229. /* This function re-construct the buffers.
  1230. Concretely, convert to wide character in case of pstr->mb_cur_max > 1,
  1231. convert to upper case in case of REG_ICASE, apply translation. */
  1232. static reg_errcode_t
  1233. internal_function
  1234. re_string_reconstruct (re_string_t *pstr, int idx, int eflags)
  1235. {
  1236. int offset = idx - pstr->raw_mbs_idx;
  1237. if (BE (offset < 0, 0))
  1238. {
  1239. /* Reset buffer. */
  1240. #ifdef RE_ENABLE_I18N
  1241. if (pstr->mb_cur_max > 1)
  1242. memset (&pstr->cur_state, '\0', sizeof (mbstate_t));
  1243. #endif /* RE_ENABLE_I18N */
  1244. pstr->len = pstr->raw_len;
  1245. pstr->stop = pstr->raw_stop;
  1246. pstr->valid_len = 0;
  1247. pstr->raw_mbs_idx = 0;
  1248. pstr->valid_raw_len = 0;
  1249. pstr->offsets_needed = 0;
  1250. pstr->tip_context = ((eflags & REG_NOTBOL) ? CONTEXT_BEGBUF
  1251. : CONTEXT_NEWLINE | CONTEXT_BEGBUF);
  1252. if (!pstr->mbs_allocated)
  1253. pstr->mbs = (unsigned char *) pstr->raw_mbs;
  1254. offset = idx;
  1255. }
  1256. if (BE (offset != 0, 1))
  1257. {
  1258. /* Should the already checked characters be kept? */
  1259. if (BE (offset < pstr->valid_raw_len, 1))
  1260. {
  1261. /* Yes, move them to the front of the buffer. */
  1262. #ifdef RE_ENABLE_I18N
  1263. if (BE (pstr->offsets_needed, 0))
  1264. {
  1265. int low = 0, high = pstr->valid_len, mid;
  1266. do
  1267. {
  1268. mid = (high + low) / 2;
  1269. if (pstr->offsets[mid] > offset)
  1270. high = mid;
  1271. else if (pstr->offsets[mid] < offset)
  1272. low = mid + 1;
  1273. else
  1274. break;
  1275. }
  1276. while (low < high);
  1277. if (pstr->offsets[mid] < offset)
  1278. ++mid;
  1279. pstr->tip_context = re_string_context_at (pstr, mid - 1,
  1280. eflags);
  1281. /* This can be quite complicated, so handle specially
  1282. only the common and easy case where the character with
  1283. different length representation of lower and upper
  1284. case is present at or after offset. */
  1285. if (pstr->valid_len > offset
  1286. && mid == offset && pstr->offsets[mid] == offset)
  1287. {
  1288. memmove (pstr->wcs, pstr->wcs + offset,
  1289. (pstr->valid_len - offset) * sizeof (wint_t));
  1290. memmove (pstr->mbs, pstr->mbs + offset, pstr->valid_len - offset);
  1291. pstr->valid_len -= offset;
  1292. pstr->valid_raw_len -= offset;
  1293. for (low = 0; low < pstr->valid_len; low++)
  1294. pstr->offsets[low] = pstr->offsets[low + offset] - offset;
  1295. }
  1296. else
  1297. {
  1298. /* Otherwise, just find out how long the partial multibyte
  1299. character at offset is and fill it with WEOF/255. */
  1300. pstr->len = pstr->raw_len - idx + offset;
  1301. pstr->stop = pstr->raw_stop - idx + offset;
  1302. pstr->offsets_needed = 0;
  1303. while (mid > 0 && pstr->offsets[mid - 1] == offset)
  1304. --mid;
  1305. while (mid < pstr->valid_len)
  1306. if (pstr->wcs[mid] != WEOF)
  1307. break;
  1308. else
  1309. ++mid;
  1310. if (mid == pstr->valid_len)
  1311. pstr->valid_len = 0;
  1312. else
  1313. {
  1314. pstr->valid_len = pstr->offsets[mid] - offset;
  1315. if (pstr->valid_len)
  1316. {
  1317. for (low = 0; low < pstr->valid_len; ++low)
  1318. pstr->wcs[low] = WEOF;
  1319. memset (pstr->mbs, 255, pstr->valid_len);
  1320. }
  1321. }
  1322. pstr->valid_raw_len = pstr->valid_len;
  1323. }
  1324. }
  1325. else
  1326. #endif
  1327. {
  1328. pstr->tip_context = re_string_context_at (pstr, offset - 1,
  1329. eflags);
  1330. #ifdef RE_ENABLE_I18N
  1331. if (pstr->mb_cur_max > 1)
  1332. memmove (pstr->wcs, pstr->wcs + offset,
  1333. (pstr->valid_len - offset) * sizeof (wint_t));
  1334. #endif /* RE_ENABLE_I18N */
  1335. if (BE (pstr->mbs_allocated, 0))
  1336. memmove (pstr->mbs, pstr->mbs + offset,
  1337. pstr->valid_len - offset);
  1338. pstr->valid_len -= offset;
  1339. pstr->valid_raw_len -= offset;
  1340. #if DEBUG
  1341. assert (pstr->valid_len > 0);
  1342. #endif
  1343. }
  1344. }
  1345. else
  1346. {
  1347. /* No, skip all characters until IDX. */
  1348. int prev_valid_len = pstr->valid_len;
  1349. #ifdef RE_ENABLE_I18N
  1350. if (BE (pstr->offsets_needed, 0))
  1351. {
  1352. pstr->len = pstr->raw_len - idx + offset;
  1353. pstr->stop = pstr->raw_stop - idx + offset;
  1354. pstr->offsets_needed = 0;
  1355. }
  1356. #endif
  1357. pstr->valid_len = 0;
  1358. #ifdef RE_ENABLE_I18N
  1359. if (pstr->mb_cur_max > 1)
  1360. {
  1361. int wcs_idx;
  1362. wint_t wc = WEOF;
  1363. if (pstr->is_utf8)
  1364. {
  1365. const unsigned char *raw, *p, *q, *end;
  1366. /* Special case UTF-8. Multi-byte chars start with any
  1367. byte other than 0x80 - 0xbf. */
  1368. raw = pstr->raw_mbs + pstr->raw_mbs_idx;
  1369. end = raw + (offset - pstr->mb_cur_max);
  1370. if (end < pstr->raw_mbs)
  1371. end = pstr->raw_mbs;
  1372. p = raw + offset - 1;
  1373. #ifdef _LIBC
  1374. /* We know the wchar_t encoding is UCS4, so for the simple
  1375. case, ASCII characters, skip the conversion step. */
  1376. if (isascii (*p) && BE (pstr->trans == NULL, 1))
  1377. {
  1378. memset (&pstr->cur_state, '\0', sizeof (mbstate_t));
  1379. /* pstr->valid_len = 0; */
  1380. wc = (wchar_t) *p;
  1381. }
  1382. else
  1383. #endif
  1384. for (; p >= end; --p)
  1385. if ((*p & 0xc0) != 0x80)
  1386. {
  1387. mbstate_t cur_state;
  1388. wchar_t wc2;
  1389. int mlen = raw + pstr->len - p;
  1390. unsigned char buf[6];
  1391. size_t mbclen;
  1392. q = p;
  1393. if (BE (pstr->trans != NULL, 0))
  1394. {
  1395. int i = mlen < 6 ? mlen : 6;
  1396. while (--i >= 0)
  1397. buf[i] = pstr->trans[p[i]];
  1398. q = buf;
  1399. }
  1400. /* XXX Don't use mbrtowc, we know which conversion
  1401. to use (UTF-8 -> UCS4). */
  1402. memset (&cur_state, 0, sizeof (cur_state));
  1403. mbclen = mbrtowc (&wc2, (const char *) p, mlen,
  1404. &cur_state);
  1405. if (raw + offset - p <= mbclen
  1406. && mbclen < (size_t) -2)
  1407. {
  1408. memset (&pstr->cur_state, '\0',
  1409. sizeof (mbstate_t));
  1410. pstr->valid_len = mbclen - (raw + offset - p);
  1411. wc = wc2;
  1412. }
  1413. break;
  1414. }
  1415. }
  1416. if (wc == WEOF)
  1417. pstr->valid_len = re_string_skip_chars (pstr, idx, &wc) - idx;
  1418. if (wc == WEOF)
  1419. pstr->tip_context
  1420. = re_string_context_at (pstr, prev_valid_len - 1, eflags);
  1421. else
  1422. pstr->tip_context = ((BE (pstr->word_ops_used != 0, 0)
  1423. && IS_WIDE_WORD_CHAR (wc))
  1424. ? CONTEXT_WORD
  1425. : ((IS_WIDE_NEWLINE (wc)
  1426. && pstr->newline_anchor)
  1427. ? CONTEXT_NEWLINE : 0));
  1428. if (BE (pstr->valid_len, 0))
  1429. {
  1430. for (wcs_idx = 0; wcs_idx < pstr->valid_len; ++wcs_idx)
  1431. pstr->wcs[wcs_idx] = WEOF;
  1432. if (pstr->mbs_allocated)
  1433. memset (pstr->mbs, 255, pstr->valid_len);
  1434. }
  1435. pstr->valid_raw_len = pstr->valid_len;
  1436. }
  1437. else
  1438. #endif /* RE_ENABLE_I18N */
  1439. {
  1440. int c = pstr->raw_mbs[pstr->raw_mbs_idx + offset - 1];
  1441. pstr->valid_raw_len = 0;
  1442. if (pstr->trans)
  1443. c = pstr->trans[c];
  1444. pstr->tip_context = (bitset_contain (pstr->word_char, c)
  1445. ? CONTEXT_WORD
  1446. : ((IS_NEWLINE (c) && pstr->newline_anchor)
  1447. ? CONTEXT_NEWLINE : 0));
  1448. }
  1449. }
  1450. if (!BE (pstr->mbs_allocated, 0))
  1451. pstr->mbs += offset;
  1452. }
  1453. pstr->raw_mbs_idx = idx;
  1454. pstr->len -= offset;
  1455. pstr->stop -= offset;
  1456. /* Then build the buffers. */
  1457. #ifdef RE_ENABLE_I18N
  1458. if (pstr->mb_cur_max > 1)
  1459. {
  1460. if (pstr->icase)
  1461. {
  1462. reg_errcode_t ret = build_wcs_upper_buffer (pstr);
  1463. if (BE (ret != REG_NOERROR, 0))
  1464. return ret;
  1465. }
  1466. else
  1467. build_wcs_buffer (pstr);
  1468. }
  1469. else
  1470. #endif /* RE_ENABLE_I18N */
  1471. if (BE (pstr->mbs_allocated, 0))
  1472. {
  1473. if (pstr->icase)
  1474. build_upper_buffer (pstr);
  1475. else if (pstr->trans != NULL)
  1476. re_string_translate_buffer (pstr);
  1477. }
  1478. else
  1479. pstr->valid_len = pstr->len;
  1480. pstr->cur_idx = 0;
  1481. return REG_NOERROR;
  1482. }
  1483. static unsigned char
  1484. internal_function __attribute ((pure))
  1485. re_string_peek_byte_case (const re_string_t *pstr, int idx)
  1486. {
  1487. int ch, off;
  1488. /* Handle the common (easiest) cases first. */
  1489. if (BE (!pstr->mbs_allocated, 1))
  1490. return re_string_peek_byte (pstr, idx);
  1491. #ifdef RE_ENABLE_I18N
  1492. if (pstr->mb_cur_max > 1
  1493. && ! re_string_is_single_byte_char (pstr, pstr->cur_idx + idx))
  1494. return re_string_peek_byte (pstr, idx);
  1495. #endif
  1496. off = pstr->cur_idx + idx;
  1497. #ifdef RE_ENABLE_I18N
  1498. if (pstr->offsets_needed)
  1499. off = pstr->offsets[off];
  1500. #endif
  1501. ch = pstr->raw_mbs[pstr->raw_mbs_idx + off];
  1502. #ifdef RE_ENABLE_I18N
  1503. /* Ensure that e.g. for tr_TR.UTF-8 BACKSLASH DOTLESS SMALL LETTER I
  1504. this function returns CAPITAL LETTER I instead of first byte of
  1505. DOTLESS SMALL LETTER I. The latter would confuse the parser,
  1506. since peek_byte_case doesn't advance cur_idx in any way. */
  1507. if (pstr->offsets_needed && !isascii (ch))
  1508. return re_string_peek_byte (pstr, idx);
  1509. #endif
  1510. return ch;
  1511. }
  1512. static unsigned char
  1513. internal_function __attribute ((pure))
  1514. re_string_fetch_byte_case (re_string_t *pstr)
  1515. {
  1516. if (BE (!pstr->mbs_allocated, 1))
  1517. return re_string_fetch_byte (pstr);
  1518. #ifdef RE_ENABLE_I18N
  1519. if (pstr->offsets_needed)
  1520. {
  1521. int off, ch;
  1522. /* For tr_TR.UTF-8 [[:islower:]] there is
  1523. [[: CAPITAL LETTER I WITH DOT lower:]] in mbs. Skip
  1524. in that case the whole multi-byte character and return
  1525. the original letter. On the other side, with
  1526. [[: DOTLESS SMALL LETTER I return [[:I, as doing
  1527. anything else would complicate things too much. */
  1528. if (!re_string_first_byte (pstr, pstr->cur_idx))
  1529. return re_string_fetch_byte (pstr);
  1530. off = pstr->offsets[pstr->cur_idx];
  1531. ch = pstr->raw_mbs[pstr->raw_mbs_idx + off];
  1532. if (! isascii (ch))
  1533. return re_string_fetch_byte (pstr);
  1534. re_string_skip_bytes (pstr,
  1535. re_string_char_size_at (pstr, pstr->cur_idx));
  1536. return ch;
  1537. }
  1538. #endif
  1539. return pstr->raw_mbs[pstr->raw_mbs_idx + pstr->cur_idx++];
  1540. }
  1541. static void
  1542. internal_function
  1543. re_string_destruct (re_string_t *pstr)
  1544. {
  1545. #ifdef RE_ENABLE_I18N
  1546. re_free (pstr->wcs);
  1547. re_free (pstr->offsets);
  1548. #endif /* RE_ENABLE_I18N */
  1549. if (pstr->mbs_allocated)
  1550. re_free (pstr->mbs);
  1551. }
  1552. /* Return the context at IDX in INPUT. */
  1553. static unsigned int
  1554. internal_function
  1555. re_string_context_at (const re_string_t *input, int idx, int eflags)
  1556. {
  1557. int c;
  1558. if (BE (idx < 0, 0))
  1559. /* In this case, we use the value stored in input->tip_context,
  1560. since we can't know the character in input->mbs[-1] here. */
  1561. return input->tip_context;
  1562. if (BE (idx == input->len, 0))
  1563. return ((eflags & REG_NOTEOL) ? CONTEXT_ENDBUF

Large files files are truncated, but you can click here to view the full file