regex_internal.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737
  1. /* Extended regular expression matching and search library.
  2. Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
  3. Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  5. Contributed by Isamu Hasegawa <isamu@yamato.ibm.com>.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3, or (at your option)
  9. any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License along
  15. with this program; if not, write to the Free Software Foundation,
  16. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  17. static void re_string_construct_common (const char *str, Idx len,
  18. re_string_t *pstr,
  19. RE_TRANSLATE_TYPE trans, bool icase,
  20. const re_dfa_t *dfa) internal_function;
  21. static re_dfastate_t *create_ci_newstate (const re_dfa_t *dfa,
  22. const re_node_set *nodes,
  23. re_hashval_t hash) internal_function;
  24. static re_dfastate_t *create_cd_newstate (const re_dfa_t *dfa,
  25. const re_node_set *nodes,
  26. unsigned int context,
  27. re_hashval_t hash) internal_function;
  28. /* Functions for string operation. */
  29. /* This function allocate the buffers. It is necessary to call
  30. re_string_reconstruct before using the object. */
  31. static reg_errcode_t
  32. internal_function
  33. re_string_allocate (re_string_t *pstr, const char *str, Idx len, Idx init_len,
  34. RE_TRANSLATE_TYPE trans, bool icase, const re_dfa_t *dfa)
  35. {
  36. reg_errcode_t ret;
  37. Idx init_buf_len;
  38. /* Ensure at least one character fits into the buffers. */
  39. if (init_len < dfa->mb_cur_max)
  40. init_len = dfa->mb_cur_max;
  41. init_buf_len = (len + 1 < init_len) ? len + 1: init_len;
  42. re_string_construct_common (str, len, pstr, trans, icase, dfa);
  43. ret = re_string_realloc_buffers (pstr, init_buf_len);
  44. if (BE (ret != REG_NOERROR, 0))
  45. return ret;
  46. pstr->word_char = dfa->word_char;
  47. pstr->word_ops_used = dfa->word_ops_used;
  48. pstr->mbs = pstr->mbs_allocated ? pstr->mbs : (unsigned char *) str;
  49. pstr->valid_len = (pstr->mbs_allocated || dfa->mb_cur_max > 1) ? 0 : len;
  50. pstr->valid_raw_len = pstr->valid_len;
  51. return REG_NOERROR;
  52. }
  53. /* This function allocate the buffers, and initialize them. */
  54. static reg_errcode_t
  55. internal_function
  56. re_string_construct (re_string_t *pstr, const char *str, Idx len,
  57. RE_TRANSLATE_TYPE trans, bool icase, const re_dfa_t *dfa)
  58. {
  59. reg_errcode_t ret;
  60. memset (pstr, '\0', sizeof (re_string_t));
  61. re_string_construct_common (str, len, pstr, trans, icase, dfa);
  62. if (len > 0)
  63. {
  64. ret = re_string_realloc_buffers (pstr, len + 1);
  65. if (BE (ret != REG_NOERROR, 0))
  66. return ret;
  67. }
  68. pstr->mbs = pstr->mbs_allocated ? pstr->mbs : (unsigned char *) str;
  69. if (icase)
  70. {
  71. #ifdef RE_ENABLE_I18N
  72. if (dfa->mb_cur_max > 1)
  73. {
  74. while (1)
  75. {
  76. ret = build_wcs_upper_buffer (pstr);
  77. if (BE (ret != REG_NOERROR, 0))
  78. return ret;
  79. if (pstr->valid_raw_len >= len)
  80. break;
  81. if (pstr->bufs_len > pstr->valid_len + dfa->mb_cur_max)
  82. break;
  83. ret = re_string_realloc_buffers (pstr, pstr->bufs_len * 2);
  84. if (BE (ret != REG_NOERROR, 0))
  85. return ret;
  86. }
  87. }
  88. else
  89. #endif /* RE_ENABLE_I18N */
  90. build_upper_buffer (pstr);
  91. }
  92. else
  93. {
  94. #ifdef RE_ENABLE_I18N
  95. if (dfa->mb_cur_max > 1)
  96. build_wcs_buffer (pstr);
  97. else
  98. #endif /* RE_ENABLE_I18N */
  99. {
  100. if (trans != NULL)
  101. re_string_translate_buffer (pstr);
  102. else
  103. {
  104. pstr->valid_len = pstr->bufs_len;
  105. pstr->valid_raw_len = pstr->bufs_len;
  106. }
  107. }
  108. }
  109. return REG_NOERROR;
  110. }
  111. /* Helper functions for re_string_allocate, and re_string_construct. */
  112. static reg_errcode_t
  113. internal_function
  114. re_string_realloc_buffers (re_string_t *pstr, Idx new_buf_len)
  115. {
  116. #ifdef RE_ENABLE_I18N
  117. if (pstr->mb_cur_max > 1)
  118. {
  119. wint_t *new_wcs;
  120. /* Avoid overflow. */
  121. size_t max_object_size = MAX (sizeof (wint_t), sizeof (Idx));
  122. if (BE (SIZE_MAX / max_object_size < new_buf_len, 0))
  123. return REG_ESPACE;
  124. new_wcs = re_realloc (pstr->wcs, wint_t, new_buf_len);
  125. if (BE (new_wcs == NULL, 0))
  126. return REG_ESPACE;
  127. pstr->wcs = new_wcs;
  128. if (pstr->offsets != NULL)
  129. {
  130. Idx *new_offsets = re_realloc (pstr->offsets, Idx, new_buf_len);
  131. if (BE (new_offsets == NULL, 0))
  132. return REG_ESPACE;
  133. pstr->offsets = new_offsets;
  134. }
  135. }
  136. #endif /* RE_ENABLE_I18N */
  137. if (pstr->mbs_allocated)
  138. {
  139. unsigned char *new_mbs = re_realloc (pstr->mbs, unsigned char,
  140. new_buf_len);
  141. if (BE (new_mbs == NULL, 0))
  142. return REG_ESPACE;
  143. pstr->mbs = new_mbs;
  144. }
  145. pstr->bufs_len = new_buf_len;
  146. return REG_NOERROR;
  147. }
  148. static void
  149. internal_function
  150. re_string_construct_common (const char *str, Idx len, re_string_t *pstr,
  151. RE_TRANSLATE_TYPE trans, bool icase,
  152. const re_dfa_t *dfa)
  153. {
  154. pstr->raw_mbs = (const unsigned char *) str;
  155. pstr->len = len;
  156. pstr->raw_len = len;
  157. pstr->trans = trans;
  158. pstr->icase = icase;
  159. pstr->mbs_allocated = (trans != NULL || icase);
  160. pstr->mb_cur_max = dfa->mb_cur_max;
  161. pstr->is_utf8 = dfa->is_utf8;
  162. pstr->map_notascii = dfa->map_notascii;
  163. pstr->stop = pstr->len;
  164. pstr->raw_stop = pstr->stop;
  165. }
  166. #ifdef RE_ENABLE_I18N
  167. /* Build wide character buffer PSTR->WCS.
  168. If the byte sequence of the string are:
  169. <mb1>(0), <mb1>(1), <mb2>(0), <mb2>(1), <sb3>
  170. Then wide character buffer will be:
  171. <wc1> , WEOF , <wc2> , WEOF , <wc3>
  172. We use WEOF for padding, they indicate that the position isn't
  173. a first byte of a multibyte character.
  174. Note that this function assumes PSTR->VALID_LEN elements are already
  175. built and starts from PSTR->VALID_LEN. */
  176. static void
  177. internal_function
  178. build_wcs_buffer (re_string_t *pstr)
  179. {
  180. #ifdef _LIBC
  181. unsigned char buf[MB_LEN_MAX];
  182. assert (MB_LEN_MAX >= pstr->mb_cur_max);
  183. #else
  184. unsigned char buf[64];
  185. #endif
  186. mbstate_t prev_st;
  187. Idx byte_idx, end_idx, remain_len;
  188. size_t mbclen;
  189. /* Build the buffers from pstr->valid_len to either pstr->len or
  190. pstr->bufs_len. */
  191. end_idx = (pstr->bufs_len > pstr->len) ? pstr->len : pstr->bufs_len;
  192. for (byte_idx = pstr->valid_len; byte_idx < end_idx;)
  193. {
  194. wchar_t wc;
  195. const char *p;
  196. remain_len = end_idx - byte_idx;
  197. prev_st = pstr->cur_state;
  198. /* Apply the translation if we need. */
  199. if (BE (pstr->trans != NULL, 0))
  200. {
  201. int i, ch;
  202. for (i = 0; i < pstr->mb_cur_max && i < remain_len; ++i)
  203. {
  204. ch = pstr->raw_mbs [pstr->raw_mbs_idx + byte_idx + i];
  205. buf[i] = pstr->mbs[byte_idx + i] = pstr->trans[ch];
  206. }
  207. p = (const char *) buf;
  208. }
  209. else
  210. p = (const char *) pstr->raw_mbs + pstr->raw_mbs_idx + byte_idx;
  211. mbclen = __mbrtowc (&wc, p, remain_len, &pstr->cur_state);
  212. if (BE (mbclen == (size_t) -2, 0))
  213. {
  214. /* The buffer doesn't have enough space, finish to build. */
  215. pstr->cur_state = prev_st;
  216. break;
  217. }
  218. else if (BE (mbclen == (size_t) -1 || mbclen == 0, 0))
  219. {
  220. /* We treat these cases as a singlebyte character. */
  221. mbclen = 1;
  222. wc = (wchar_t) pstr->raw_mbs[pstr->raw_mbs_idx + byte_idx];
  223. if (BE (pstr->trans != NULL, 0))
  224. wc = pstr->trans[wc];
  225. pstr->cur_state = prev_st;
  226. }
  227. /* Write wide character and padding. */
  228. pstr->wcs[byte_idx++] = wc;
  229. /* Write paddings. */
  230. for (remain_len = byte_idx + mbclen - 1; byte_idx < remain_len ;)
  231. pstr->wcs[byte_idx++] = WEOF;
  232. }
  233. pstr->valid_len = byte_idx;
  234. pstr->valid_raw_len = byte_idx;
  235. }
  236. /* Build wide character buffer PSTR->WCS like build_wcs_buffer,
  237. but for REG_ICASE. */
  238. static reg_errcode_t
  239. internal_function
  240. build_wcs_upper_buffer (re_string_t *pstr)
  241. {
  242. mbstate_t prev_st;
  243. Idx src_idx, byte_idx, end_idx, remain_len;
  244. size_t mbclen;
  245. #ifdef _LIBC
  246. char buf[MB_LEN_MAX];
  247. assert (MB_LEN_MAX >= pstr->mb_cur_max);
  248. #else
  249. char buf[64];
  250. #endif
  251. byte_idx = pstr->valid_len;
  252. end_idx = (pstr->bufs_len > pstr->len) ? pstr->len : pstr->bufs_len;
  253. /* The following optimization assumes that ASCII characters can be
  254. mapped to wide characters with a simple cast. */
  255. if (! pstr->map_notascii && pstr->trans == NULL && !pstr->offsets_needed)
  256. {
  257. while (byte_idx < end_idx)
  258. {
  259. wchar_t wc;
  260. if (isascii (pstr->raw_mbs[pstr->raw_mbs_idx + byte_idx])
  261. && mbsinit (&pstr->cur_state))
  262. {
  263. /* In case of a singlebyte character. */
  264. pstr->mbs[byte_idx]
  265. = toupper (pstr->raw_mbs[pstr->raw_mbs_idx + byte_idx]);
  266. /* The next step uses the assumption that wchar_t is encoded
  267. ASCII-safe: all ASCII values can be converted like this. */
  268. pstr->wcs[byte_idx] = (wchar_t) pstr->mbs[byte_idx];
  269. ++byte_idx;
  270. continue;
  271. }
  272. remain_len = end_idx - byte_idx;
  273. prev_st = pstr->cur_state;
  274. mbclen = __mbrtowc (&wc,
  275. ((const char *) pstr->raw_mbs + pstr->raw_mbs_idx
  276. + byte_idx), remain_len, &pstr->cur_state);
  277. if (BE (mbclen < (size_t) -2, 1))
  278. {
  279. wchar_t wcu = wc;
  280. if (iswlower (wc))
  281. {
  282. size_t mbcdlen;
  283. wcu = towupper (wc);
  284. mbcdlen = wcrtomb (buf, wcu, &prev_st);
  285. if (BE (mbclen == mbcdlen, 1))
  286. memcpy (pstr->mbs + byte_idx, buf, mbclen);
  287. else
  288. {
  289. src_idx = byte_idx;
  290. goto offsets_needed;
  291. }
  292. }
  293. else
  294. memcpy (pstr->mbs + byte_idx,
  295. pstr->raw_mbs + pstr->raw_mbs_idx + byte_idx, mbclen);
  296. pstr->wcs[byte_idx++] = wcu;
  297. /* Write paddings. */
  298. for (remain_len = byte_idx + mbclen - 1; byte_idx < remain_len ;)
  299. pstr->wcs[byte_idx++] = WEOF;
  300. }
  301. else if (mbclen == (size_t) -1 || mbclen == 0)
  302. {
  303. /* It is an invalid character or '\0'. Just use the byte. */
  304. int ch = pstr->raw_mbs[pstr->raw_mbs_idx + byte_idx];
  305. pstr->mbs[byte_idx] = ch;
  306. /* And also cast it to wide char. */
  307. pstr->wcs[byte_idx++] = (wchar_t) ch;
  308. if (BE (mbclen == (size_t) -1, 0))
  309. pstr->cur_state = prev_st;
  310. }
  311. else
  312. {
  313. /* The buffer doesn't have enough space, finish to build. */
  314. pstr->cur_state = prev_st;
  315. break;
  316. }
  317. }
  318. pstr->valid_len = byte_idx;
  319. pstr->valid_raw_len = byte_idx;
  320. return REG_NOERROR;
  321. }
  322. else
  323. for (src_idx = pstr->valid_raw_len; byte_idx < end_idx;)
  324. {
  325. wchar_t wc;
  326. const char *p;
  327. offsets_needed:
  328. remain_len = end_idx - byte_idx;
  329. prev_st = pstr->cur_state;
  330. if (BE (pstr->trans != NULL, 0))
  331. {
  332. int i, ch;
  333. for (i = 0; i < pstr->mb_cur_max && i < remain_len; ++i)
  334. {
  335. ch = pstr->raw_mbs [pstr->raw_mbs_idx + src_idx + i];
  336. buf[i] = pstr->trans[ch];
  337. }
  338. p = (const char *) buf;
  339. }
  340. else
  341. p = (const char *) pstr->raw_mbs + pstr->raw_mbs_idx + src_idx;
  342. mbclen = __mbrtowc (&wc, p, remain_len, &pstr->cur_state);
  343. if (BE (mbclen < (size_t) -2, 1))
  344. {
  345. wchar_t wcu = wc;
  346. if (iswlower (wc))
  347. {
  348. size_t mbcdlen;
  349. wcu = towupper (wc);
  350. mbcdlen = wcrtomb ((char *) buf, wcu, &prev_st);
  351. if (BE (mbclen == mbcdlen, 1))
  352. memcpy (pstr->mbs + byte_idx, buf, mbclen);
  353. else if (mbcdlen != (size_t) -1)
  354. {
  355. size_t i;
  356. if (byte_idx + mbcdlen > pstr->bufs_len)
  357. {
  358. pstr->cur_state = prev_st;
  359. break;
  360. }
  361. if (pstr->offsets == NULL)
  362. {
  363. pstr->offsets = re_malloc (Idx, pstr->bufs_len);
  364. if (pstr->offsets == NULL)
  365. return REG_ESPACE;
  366. }
  367. if (!pstr->offsets_needed)
  368. {
  369. for (i = 0; i < (size_t) byte_idx; ++i)
  370. pstr->offsets[i] = i;
  371. pstr->offsets_needed = 1;
  372. }
  373. memcpy (pstr->mbs + byte_idx, buf, mbcdlen);
  374. pstr->wcs[byte_idx] = wcu;
  375. pstr->offsets[byte_idx] = src_idx;
  376. for (i = 1; i < mbcdlen; ++i)
  377. {
  378. pstr->offsets[byte_idx + i]
  379. = src_idx + (i < mbclen ? i : mbclen - 1);
  380. pstr->wcs[byte_idx + i] = WEOF;
  381. }
  382. pstr->len += mbcdlen - mbclen;
  383. if (pstr->raw_stop > src_idx)
  384. pstr->stop += mbcdlen - mbclen;
  385. end_idx = (pstr->bufs_len > pstr->len)
  386. ? pstr->len : pstr->bufs_len;
  387. byte_idx += mbcdlen;
  388. src_idx += mbclen;
  389. continue;
  390. }
  391. else
  392. memcpy (pstr->mbs + byte_idx, p, mbclen);
  393. }
  394. else
  395. memcpy (pstr->mbs + byte_idx, p, mbclen);
  396. if (BE (pstr->offsets_needed != 0, 0))
  397. {
  398. size_t i;
  399. for (i = 0; i < mbclen; ++i)
  400. pstr->offsets[byte_idx + i] = src_idx + i;
  401. }
  402. src_idx += mbclen;
  403. pstr->wcs[byte_idx++] = wcu;
  404. /* Write paddings. */
  405. for (remain_len = byte_idx + mbclen - 1; byte_idx < remain_len ;)
  406. pstr->wcs[byte_idx++] = WEOF;
  407. }
  408. else if (mbclen == (size_t) -1 || mbclen == 0)
  409. {
  410. /* It is an invalid character or '\0'. Just use the byte. */
  411. int ch = pstr->raw_mbs[pstr->raw_mbs_idx + src_idx];
  412. if (BE (pstr->trans != NULL, 0))
  413. ch = pstr->trans [ch];
  414. pstr->mbs[byte_idx] = ch;
  415. if (BE (pstr->offsets_needed != 0, 0))
  416. pstr->offsets[byte_idx] = src_idx;
  417. ++src_idx;
  418. /* And also cast it to wide char. */
  419. pstr->wcs[byte_idx++] = (wchar_t) ch;
  420. if (BE (mbclen == (size_t) -1, 0))
  421. pstr->cur_state = prev_st;
  422. }
  423. else
  424. {
  425. /* The buffer doesn't have enough space, finish to build. */
  426. pstr->cur_state = prev_st;
  427. break;
  428. }
  429. }
  430. pstr->valid_len = byte_idx;
  431. pstr->valid_raw_len = src_idx;
  432. return REG_NOERROR;
  433. }
  434. /* Skip characters until the index becomes greater than NEW_RAW_IDX.
  435. Return the index. */
  436. static Idx
  437. internal_function
  438. re_string_skip_chars (re_string_t *pstr, Idx new_raw_idx, wint_t *last_wc)
  439. {
  440. mbstate_t prev_st;
  441. Idx rawbuf_idx;
  442. size_t mbclen;
  443. wint_t wc = WEOF;
  444. /* Skip the characters which are not necessary to check. */
  445. for (rawbuf_idx = pstr->raw_mbs_idx + pstr->valid_raw_len;
  446. rawbuf_idx < new_raw_idx;)
  447. {
  448. wchar_t wc2;
  449. Idx remain_len;
  450. remain_len = pstr->len - rawbuf_idx;
  451. prev_st = pstr->cur_state;
  452. mbclen = __mbrtowc (&wc2, (const char *) pstr->raw_mbs + rawbuf_idx,
  453. remain_len, &pstr->cur_state);
  454. if (BE (mbclen == (size_t) -2 || mbclen == (size_t) -1 || mbclen == 0, 0))
  455. {
  456. /* We treat these cases as a single byte character. */
  457. if (mbclen == 0 || remain_len == 0)
  458. wc = L'\0';
  459. else
  460. wc = *(unsigned char *) (pstr->raw_mbs + rawbuf_idx);
  461. mbclen = 1;
  462. pstr->cur_state = prev_st;
  463. }
  464. else
  465. wc = wc2;
  466. /* Then proceed the next character. */
  467. rawbuf_idx += mbclen;
  468. }
  469. *last_wc = wc;
  470. return rawbuf_idx;
  471. }
  472. #endif /* RE_ENABLE_I18N */
  473. /* Build the buffer PSTR->MBS, and apply the translation if we need.
  474. This function is used in case of REG_ICASE. */
  475. static void
  476. internal_function
  477. build_upper_buffer (re_string_t *pstr)
  478. {
  479. Idx char_idx, end_idx;
  480. end_idx = (pstr->bufs_len > pstr->len) ? pstr->len : pstr->bufs_len;
  481. for (char_idx = pstr->valid_len; char_idx < end_idx; ++char_idx)
  482. {
  483. int ch = pstr->raw_mbs[pstr->raw_mbs_idx + char_idx];
  484. if (BE (pstr->trans != NULL, 0))
  485. ch = pstr->trans[ch];
  486. if (islower (ch))
  487. pstr->mbs[char_idx] = toupper (ch);
  488. else
  489. pstr->mbs[char_idx] = ch;
  490. }
  491. pstr->valid_len = char_idx;
  492. pstr->valid_raw_len = char_idx;
  493. }
  494. /* Apply TRANS to the buffer in PSTR. */
  495. static void
  496. internal_function
  497. re_string_translate_buffer (re_string_t *pstr)
  498. {
  499. Idx buf_idx, end_idx;
  500. end_idx = (pstr->bufs_len > pstr->len) ? pstr->len : pstr->bufs_len;
  501. for (buf_idx = pstr->valid_len; buf_idx < end_idx; ++buf_idx)
  502. {
  503. int ch = pstr->raw_mbs[pstr->raw_mbs_idx + buf_idx];
  504. pstr->mbs[buf_idx] = pstr->trans[ch];
  505. }
  506. pstr->valid_len = buf_idx;
  507. pstr->valid_raw_len = buf_idx;
  508. }
  509. /* This function re-construct the buffers.
  510. Concretely, convert to wide character in case of pstr->mb_cur_max > 1,
  511. convert to upper case in case of REG_ICASE, apply translation. */
  512. static reg_errcode_t
  513. internal_function
  514. re_string_reconstruct (re_string_t *pstr, Idx idx, int eflags)
  515. {
  516. Idx offset;
  517. if (BE (pstr->raw_mbs_idx <= idx, 0))
  518. offset = idx - pstr->raw_mbs_idx;
  519. else
  520. {
  521. /* Reset buffer. */
  522. #ifdef RE_ENABLE_I18N
  523. if (pstr->mb_cur_max > 1)
  524. memset (&pstr->cur_state, '\0', sizeof (mbstate_t));
  525. #endif /* RE_ENABLE_I18N */
  526. pstr->len = pstr->raw_len;
  527. pstr->stop = pstr->raw_stop;
  528. pstr->valid_len = 0;
  529. pstr->raw_mbs_idx = 0;
  530. pstr->valid_raw_len = 0;
  531. pstr->offsets_needed = 0;
  532. pstr->tip_context = ((eflags & REG_NOTBOL) ? CONTEXT_BEGBUF
  533. : CONTEXT_NEWLINE | CONTEXT_BEGBUF);
  534. if (!pstr->mbs_allocated)
  535. pstr->mbs = (unsigned char *) pstr->raw_mbs;
  536. offset = idx;
  537. }
  538. if (BE (offset != 0, 1))
  539. {
  540. /* Should the already checked characters be kept? */
  541. if (BE (offset < pstr->valid_raw_len, 1))
  542. {
  543. /* Yes, move them to the front of the buffer. */
  544. #ifdef RE_ENABLE_I18N
  545. if (BE (pstr->offsets_needed, 0))
  546. {
  547. Idx low = 0, high = pstr->valid_len, mid;
  548. do
  549. {
  550. mid = (high + low) / 2;
  551. if (pstr->offsets[mid] > offset)
  552. high = mid;
  553. else if (pstr->offsets[mid] < offset)
  554. low = mid + 1;
  555. else
  556. break;
  557. }
  558. while (low < high);
  559. if (pstr->offsets[mid] < offset)
  560. ++mid;
  561. pstr->tip_context = re_string_context_at (pstr, mid - 1,
  562. eflags);
  563. /* This can be quite complicated, so handle specially
  564. only the common and easy case where the character with
  565. different length representation of lower and upper
  566. case is present at or after offset. */
  567. if (pstr->valid_len > offset
  568. && mid == offset && pstr->offsets[mid] == offset)
  569. {
  570. memmove (pstr->wcs, pstr->wcs + offset,
  571. (pstr->valid_len - offset) * sizeof (wint_t));
  572. memmove (pstr->mbs, pstr->mbs + offset, pstr->valid_len - offset);
  573. pstr->valid_len -= offset;
  574. pstr->valid_raw_len -= offset;
  575. for (low = 0; low < pstr->valid_len; low++)
  576. pstr->offsets[low] = pstr->offsets[low + offset] - offset;
  577. }
  578. else
  579. {
  580. /* Otherwise, just find out how long the partial multibyte
  581. character at offset is and fill it with WEOF/255. */
  582. pstr->len = pstr->raw_len - idx + offset;
  583. pstr->stop = pstr->raw_stop - idx + offset;
  584. pstr->offsets_needed = 0;
  585. while (mid > 0 && pstr->offsets[mid - 1] == offset)
  586. --mid;
  587. while (mid < pstr->valid_len)
  588. if (pstr->wcs[mid] != WEOF)
  589. break;
  590. else
  591. ++mid;
  592. if (mid == pstr->valid_len)
  593. pstr->valid_len = 0;
  594. else
  595. {
  596. pstr->valid_len = pstr->offsets[mid] - offset;
  597. if (pstr->valid_len)
  598. {
  599. for (low = 0; low < pstr->valid_len; ++low)
  600. pstr->wcs[low] = WEOF;
  601. memset (pstr->mbs, 255, pstr->valid_len);
  602. }
  603. }
  604. pstr->valid_raw_len = pstr->valid_len;
  605. }
  606. }
  607. else
  608. #endif
  609. {
  610. pstr->tip_context = re_string_context_at (pstr, offset - 1,
  611. eflags);
  612. #ifdef RE_ENABLE_I18N
  613. if (pstr->mb_cur_max > 1)
  614. memmove (pstr->wcs, pstr->wcs + offset,
  615. (pstr->valid_len - offset) * sizeof (wint_t));
  616. #endif /* RE_ENABLE_I18N */
  617. if (BE (pstr->mbs_allocated, 0))
  618. memmove (pstr->mbs, pstr->mbs + offset,
  619. pstr->valid_len - offset);
  620. pstr->valid_len -= offset;
  621. pstr->valid_raw_len -= offset;
  622. #if DEBUG
  623. assert (pstr->valid_len > 0);
  624. #endif
  625. }
  626. }
  627. else
  628. {
  629. #ifdef RE_ENABLE_I18N
  630. /* No, skip all characters until IDX. */
  631. Idx prev_valid_len = pstr->valid_len;
  632. if (BE (pstr->offsets_needed, 0))
  633. {
  634. pstr->len = pstr->raw_len - idx + offset;
  635. pstr->stop = pstr->raw_stop - idx + offset;
  636. pstr->offsets_needed = 0;
  637. }
  638. #endif
  639. pstr->valid_len = 0;
  640. #ifdef RE_ENABLE_I18N
  641. if (pstr->mb_cur_max > 1)
  642. {
  643. Idx wcs_idx;
  644. wint_t wc = WEOF;
  645. if (pstr->is_utf8)
  646. {
  647. const unsigned char *raw, *p, *end;
  648. /* Special case UTF-8. Multi-byte chars start with any
  649. byte other than 0x80 - 0xbf. */
  650. raw = pstr->raw_mbs + pstr->raw_mbs_idx;
  651. end = raw + (offset - pstr->mb_cur_max);
  652. if (end < pstr->raw_mbs)
  653. end = pstr->raw_mbs;
  654. p = raw + offset - 1;
  655. #ifdef _LIBC
  656. /* We know the wchar_t encoding is UCS4, so for the simple
  657. case, ASCII characters, skip the conversion step. */
  658. if (isascii (*p) && BE (pstr->trans == NULL, 1))
  659. {
  660. memset (&pstr->cur_state, '\0', sizeof (mbstate_t));
  661. /* pstr->valid_len = 0; */
  662. wc = (wchar_t) *p;
  663. }
  664. else
  665. #endif
  666. for (; p >= end; --p)
  667. if ((*p & 0xc0) != 0x80)
  668. {
  669. mbstate_t cur_state;
  670. wchar_t wc2;
  671. Idx mlen = raw + pstr->len - p;
  672. unsigned char buf[6];
  673. size_t mbclen;
  674. if (BE (pstr->trans != NULL, 0))
  675. {
  676. int i = mlen < 6 ? mlen : 6;
  677. while (--i >= 0)
  678. buf[i] = pstr->trans[p[i]];
  679. }
  680. /* XXX Don't use mbrtowc, we know which conversion
  681. to use (UTF-8 -> UCS4). */
  682. memset (&cur_state, 0, sizeof (cur_state));
  683. mbclen = __mbrtowc (&wc2, (const char *) p, mlen,
  684. &cur_state);
  685. if (raw + offset - p <= mbclen
  686. && mbclen < (size_t) -2)
  687. {
  688. memset (&pstr->cur_state, '\0',
  689. sizeof (mbstate_t));
  690. pstr->valid_len = mbclen - (raw + offset - p);
  691. wc = wc2;
  692. }
  693. break;
  694. }
  695. }
  696. if (wc == WEOF)
  697. pstr->valid_len = re_string_skip_chars (pstr, idx, &wc) - idx;
  698. if (wc == WEOF)
  699. pstr->tip_context
  700. = re_string_context_at (pstr, prev_valid_len - 1, eflags);
  701. else
  702. pstr->tip_context = ((BE (pstr->word_ops_used != 0, 0)
  703. && IS_WIDE_WORD_CHAR (wc))
  704. ? CONTEXT_WORD
  705. : ((IS_WIDE_NEWLINE (wc)
  706. && pstr->newline_anchor)
  707. ? CONTEXT_NEWLINE : 0));
  708. if (BE (pstr->valid_len, 0))
  709. {
  710. for (wcs_idx = 0; wcs_idx < pstr->valid_len; ++wcs_idx)
  711. pstr->wcs[wcs_idx] = WEOF;
  712. if (pstr->mbs_allocated)
  713. memset (pstr->mbs, 255, pstr->valid_len);
  714. }
  715. pstr->valid_raw_len = pstr->valid_len;
  716. }
  717. else
  718. #endif /* RE_ENABLE_I18N */
  719. {
  720. int c = pstr->raw_mbs[pstr->raw_mbs_idx + offset - 1];
  721. pstr->valid_raw_len = 0;
  722. if (pstr->trans)
  723. c = pstr->trans[c];
  724. pstr->tip_context = (bitset_contain (pstr->word_char, c)
  725. ? CONTEXT_WORD
  726. : ((IS_NEWLINE (c) && pstr->newline_anchor)
  727. ? CONTEXT_NEWLINE : 0));
  728. }
  729. }
  730. if (!BE (pstr->mbs_allocated, 0))
  731. pstr->mbs += offset;
  732. }
  733. pstr->raw_mbs_idx = idx;
  734. pstr->len -= offset;
  735. pstr->stop -= offset;
  736. /* Then build the buffers. */
  737. #ifdef RE_ENABLE_I18N
  738. if (pstr->mb_cur_max > 1)
  739. {
  740. if (pstr->icase)
  741. {
  742. reg_errcode_t ret = build_wcs_upper_buffer (pstr);
  743. if (BE (ret != REG_NOERROR, 0))
  744. return ret;
  745. }
  746. else
  747. build_wcs_buffer (pstr);
  748. }
  749. else
  750. #endif /* RE_ENABLE_I18N */
  751. if (BE (pstr->mbs_allocated, 0))
  752. {
  753. if (pstr->icase)
  754. build_upper_buffer (pstr);
  755. else if (pstr->trans != NULL)
  756. re_string_translate_buffer (pstr);
  757. }
  758. else
  759. pstr->valid_len = pstr->len;
  760. pstr->cur_idx = 0;
  761. return REG_NOERROR;
  762. }
  763. static unsigned char
  764. internal_function __attribute ((pure))
  765. re_string_peek_byte_case (const re_string_t *pstr, Idx idx)
  766. {
  767. int ch;
  768. Idx off;
  769. /* Handle the common (easiest) cases first. */
  770. if (BE (!pstr->mbs_allocated, 1))
  771. return re_string_peek_byte (pstr, idx);
  772. #ifdef RE_ENABLE_I18N
  773. if (pstr->mb_cur_max > 1
  774. && ! re_string_is_single_byte_char (pstr, pstr->cur_idx + idx))
  775. return re_string_peek_byte (pstr, idx);
  776. #endif
  777. off = pstr->cur_idx + idx;
  778. #ifdef RE_ENABLE_I18N
  779. if (pstr->offsets_needed)
  780. off = pstr->offsets[off];
  781. #endif
  782. ch = pstr->raw_mbs[pstr->raw_mbs_idx + off];
  783. #ifdef RE_ENABLE_I18N
  784. /* Ensure that e.g. for tr_TR.UTF-8 BACKSLASH DOTLESS SMALL LETTER I
  785. this function returns CAPITAL LETTER I instead of first byte of
  786. DOTLESS SMALL LETTER I. The latter would confuse the parser,
  787. since peek_byte_case doesn't advance cur_idx in any way. */
  788. if (pstr->offsets_needed && !isascii (ch))
  789. return re_string_peek_byte (pstr, idx);
  790. #endif
  791. return ch;
  792. }
  793. static unsigned char
  794. internal_function __attribute ((pure))
  795. re_string_fetch_byte_case (re_string_t *pstr)
  796. {
  797. if (BE (!pstr->mbs_allocated, 1))
  798. return re_string_fetch_byte (pstr);
  799. #ifdef RE_ENABLE_I18N
  800. if (pstr->offsets_needed)
  801. {
  802. Idx off;
  803. int ch;
  804. /* For tr_TR.UTF-8 [[:islower:]] there is
  805. [[: CAPITAL LETTER I WITH DOT lower:]] in mbs. Skip
  806. in that case the whole multi-byte character and return
  807. the original letter. On the other side, with
  808. [[: DOTLESS SMALL LETTER I return [[:I, as doing
  809. anything else would complicate things too much. */
  810. if (!re_string_first_byte (pstr, pstr->cur_idx))
  811. return re_string_fetch_byte (pstr);
  812. off = pstr->offsets[pstr->cur_idx];
  813. ch = pstr->raw_mbs[pstr->raw_mbs_idx + off];
  814. if (! isascii (ch))
  815. return re_string_fetch_byte (pstr);
  816. re_string_skip_bytes (pstr,
  817. re_string_char_size_at (pstr, pstr->cur_idx));
  818. return ch;
  819. }
  820. #endif
  821. return pstr->raw_mbs[pstr->raw_mbs_idx + pstr->cur_idx++];
  822. }
  823. static void
  824. internal_function
  825. re_string_destruct (re_string_t *pstr)
  826. {
  827. #ifdef RE_ENABLE_I18N
  828. re_free (pstr->wcs);
  829. re_free (pstr->offsets);
  830. #endif /* RE_ENABLE_I18N */
  831. if (pstr->mbs_allocated)
  832. re_free (pstr->mbs);
  833. }
  834. /* Return the context at IDX in INPUT. */
  835. static unsigned int
  836. internal_function
  837. re_string_context_at (const re_string_t *input, Idx idx, int eflags)
  838. {
  839. int c;
  840. if (BE (! REG_VALID_INDEX (idx), 0))
  841. /* In this case, we use the value stored in input->tip_context,
  842. since we can't know the character in input->mbs[-1] here. */
  843. return input->tip_context;
  844. if (BE (idx == input->len, 0))
  845. return ((eflags & REG_NOTEOL) ? CONTEXT_ENDBUF
  846. : CONTEXT_NEWLINE | CONTEXT_ENDBUF);
  847. #ifdef RE_ENABLE_I18N
  848. if (input->mb_cur_max > 1)
  849. {
  850. wint_t wc;
  851. Idx wc_idx = idx;
  852. while(input->wcs[wc_idx] == WEOF)
  853. {
  854. #ifdef DEBUG
  855. /* It must not happen. */
  856. assert (REG_VALID_INDEX (wc_idx));
  857. #endif
  858. --wc_idx;
  859. if (! REG_VALID_INDEX (wc_idx))
  860. return input->tip_context;
  861. }
  862. wc = input->wcs[wc_idx];
  863. if (BE (input->word_ops_used != 0, 0) && IS_WIDE_WORD_CHAR (wc))
  864. return CONTEXT_WORD;
  865. return (IS_WIDE_NEWLINE (wc) && input->newline_anchor
  866. ? CONTEXT_NEWLINE : 0);
  867. }
  868. else
  869. #endif
  870. {
  871. c = re_string_byte_at (input, idx);
  872. if (bitset_contain (input->word_char, c))
  873. return CONTEXT_WORD;
  874. return IS_NEWLINE (c) && input->newline_anchor ? CONTEXT_NEWLINE : 0;
  875. }
  876. }
  877. /* Functions for set operation. */
  878. static reg_errcode_t
  879. internal_function
  880. re_node_set_alloc (re_node_set *set, Idx size)
  881. {
  882. set->alloc = size;
  883. set->nelem = 0;
  884. set->elems = re_malloc (Idx, size);
  885. if (BE (set->elems == NULL, 0))
  886. return REG_ESPACE;
  887. return REG_NOERROR;
  888. }
  889. static reg_errcode_t
  890. internal_function
  891. re_node_set_init_1 (re_node_set *set, Idx elem)
  892. {
  893. set->alloc = 1;
  894. set->nelem = 1;
  895. set->elems = re_malloc (Idx, 1);
  896. if (BE (set->elems == NULL, 0))
  897. {
  898. set->alloc = set->nelem = 0;
  899. return REG_ESPACE;
  900. }
  901. set->elems[0] = elem;
  902. return REG_NOERROR;
  903. }
  904. static reg_errcode_t
  905. internal_function
  906. re_node_set_init_2 (re_node_set *set, Idx elem1, Idx elem2)
  907. {
  908. set->alloc = 2;
  909. set->elems = re_malloc (Idx, 2);
  910. if (BE (set->elems == NULL, 0))
  911. return REG_ESPACE;
  912. if (elem1 == elem2)
  913. {
  914. set->nelem = 1;
  915. set->elems[0] = elem1;
  916. }
  917. else
  918. {
  919. set->nelem = 2;
  920. if (elem1 < elem2)
  921. {
  922. set->elems[0] = elem1;
  923. set->elems[1] = elem2;
  924. }
  925. else
  926. {
  927. set->elems[0] = elem2;
  928. set->elems[1] = elem1;
  929. }
  930. }
  931. return REG_NOERROR;
  932. }
  933. static reg_errcode_t
  934. internal_function
  935. re_node_set_init_copy (re_node_set *dest, const re_node_set *src)
  936. {
  937. dest->nelem = src->nelem;
  938. if (src->nelem > 0)
  939. {
  940. dest->alloc = dest->nelem;
  941. dest->elems = re_malloc (Idx, dest->alloc);
  942. if (BE (dest->elems == NULL, 0))
  943. {
  944. dest->alloc = dest->nelem = 0;
  945. return REG_ESPACE;
  946. }
  947. memcpy (dest->elems, src->elems, src->nelem * sizeof (Idx));
  948. }
  949. else
  950. re_node_set_init_empty (dest);
  951. return REG_NOERROR;
  952. }
  953. /* Calculate the intersection of the sets SRC1 and SRC2. And merge it to
  954. DEST. Return value indicate the error code or REG_NOERROR if succeeded.
  955. Note: We assume dest->elems is NULL, when dest->alloc is 0. */
  956. static reg_errcode_t
  957. internal_function
  958. re_node_set_add_intersect (re_node_set *dest, const re_node_set *src1,
  959. const re_node_set *src2)
  960. {
  961. Idx i1, i2, is, id, delta, sbase;
  962. if (src1->nelem == 0 || src2->nelem == 0)
  963. return REG_NOERROR;
  964. /* We need dest->nelem + 2 * elems_in_intersection; this is a
  965. conservative estimate. */
  966. if (src1->nelem + src2->nelem + dest->nelem > dest->alloc)
  967. {
  968. Idx new_alloc = src1->nelem + src2->nelem + dest->alloc;
  969. Idx *new_elems = re_realloc (dest->elems, Idx, new_alloc);
  970. if (BE (new_elems == NULL, 0))
  971. return REG_ESPACE;
  972. dest->elems = new_elems;
  973. dest->alloc = new_alloc;
  974. }
  975. /* Find the items in the intersection of SRC1 and SRC2, and copy
  976. into the top of DEST those that are not already in DEST itself. */
  977. sbase = dest->nelem + src1->nelem + src2->nelem;
  978. i1 = src1->nelem - 1;
  979. i2 = src2->nelem - 1;
  980. id = dest->nelem - 1;
  981. for (;;)
  982. {
  983. if (src1->elems[i1] == src2->elems[i2])
  984. {
  985. /* Try to find the item in DEST. Maybe we could binary search? */
  986. while (REG_VALID_INDEX (id) && dest->elems[id] > src1->elems[i1])
  987. --id;
  988. if (! REG_VALID_INDEX (id) || dest->elems[id] != src1->elems[i1])
  989. dest->elems[--sbase] = src1->elems[i1];
  990. if (! REG_VALID_INDEX (--i1) || ! REG_VALID_INDEX (--i2))
  991. break;
  992. }
  993. /* Lower the highest of the two items. */
  994. else if (src1->elems[i1] < src2->elems[i2])
  995. {
  996. if (! REG_VALID_INDEX (--i2))
  997. break;
  998. }
  999. else
  1000. {
  1001. if (! REG_VALID_INDEX (--i1))
  1002. break;
  1003. }
  1004. }
  1005. id = dest->nelem - 1;
  1006. is = dest->nelem + src1->nelem + src2->nelem - 1;
  1007. delta = is - sbase + 1;
  1008. /* Now copy. When DELTA becomes zero, the remaining
  1009. DEST elements are already in place; this is more or
  1010. less the same loop that is in re_node_set_merge. */
  1011. dest->nelem += delta;
  1012. if (delta > 0 && REG_VALID_INDEX (id))
  1013. for (;;)
  1014. {
  1015. if (dest->elems[is] > dest->elems[id])
  1016. {
  1017. /* Copy from the top. */
  1018. dest->elems[id + delta--] = dest->elems[is--];
  1019. if (delta == 0)
  1020. break;
  1021. }
  1022. else
  1023. {
  1024. /* Slide from the bottom. */
  1025. dest->elems[id + delta] = dest->elems[id];
  1026. if (! REG_VALID_INDEX (--id))
  1027. break;
  1028. }
  1029. }
  1030. /* Copy remaining SRC elements. */
  1031. memcpy (dest->elems, dest->elems + sbase, delta * sizeof (Idx));
  1032. return REG_NOERROR;
  1033. }
  1034. /* Calculate the union set of the sets SRC1 and SRC2. And store it to
  1035. DEST. Return value indicate the error code or REG_NOERROR if succeeded. */
  1036. static reg_errcode_t
  1037. internal_function
  1038. re_node_set_init_union (re_node_set *dest, const re_node_set *src1,
  1039. const re_node_set *src2)
  1040. {
  1041. Idx i1, i2, id;
  1042. if (src1 != NULL && src1->nelem > 0 && src2 != NULL && src2->nelem > 0)
  1043. {
  1044. dest->alloc = src1->nelem + src2->nelem;
  1045. dest->elems = re_malloc (Idx, dest->alloc);
  1046. if (BE (dest->elems == NULL, 0))
  1047. return REG_ESPACE;
  1048. }
  1049. else
  1050. {
  1051. if (src1 != NULL && src1->nelem > 0)
  1052. return re_node_set_init_copy (dest, src1);
  1053. else if (src2 != NULL && src2->nelem > 0)
  1054. return re_node_set_init_copy (dest, src2);
  1055. else
  1056. re_node_set_init_empty (dest);
  1057. return REG_NOERROR;
  1058. }
  1059. for (i1 = i2 = id = 0 ; i1 < src1->nelem && i2 < src2->nelem ;)
  1060. {
  1061. if (src1->elems[i1] > src2->elems[i2])
  1062. {
  1063. dest->elems[id++] = src2->elems[i2++];
  1064. continue;
  1065. }
  1066. if (src1->elems[i1] == src2->elems[i2])
  1067. ++i2;
  1068. dest->elems[id++] = src1->elems[i1++];
  1069. }
  1070. if (i1 < src1->nelem)
  1071. {
  1072. memcpy (dest->elems + id, src1->elems + i1,
  1073. (src1->nelem - i1) * sizeof (Idx));
  1074. id += src1->nelem - i1;
  1075. }
  1076. else if (i2 < src2->nelem)
  1077. {
  1078. memcpy (dest->elems + id, src2->elems + i2,
  1079. (src2->nelem - i2) * sizeof (Idx));
  1080. id += src2->nelem - i2;
  1081. }
  1082. dest->nelem = id;
  1083. return REG_NOERROR;
  1084. }
  1085. /* Calculate the union set of the sets DEST and SRC. And store it to
  1086. DEST. Return value indicate the error code or REG_NOERROR if succeeded. */
  1087. static reg_errcode_t
  1088. internal_function
  1089. re_node_set_merge (re_node_set *dest, const re_node_set *src)
  1090. {
  1091. Idx is, id, sbase, delta;
  1092. if (src == NULL || src->nelem == 0)
  1093. return REG_NOERROR;
  1094. if (dest->alloc < 2 * src->nelem + dest->nelem)
  1095. {
  1096. Idx new_alloc = 2 * (src->nelem + dest->alloc);
  1097. Idx *new_buffer = re_realloc (dest->elems, Idx, new_alloc);
  1098. if (BE (new_buffer == NULL, 0))
  1099. return REG_ESPACE;
  1100. dest->elems = new_buffer;
  1101. dest->alloc = new_alloc;
  1102. }
  1103. if (BE (dest->nelem == 0, 0))
  1104. {
  1105. dest->nelem = src->nelem;
  1106. memcpy (dest->elems, src->elems, src->nelem * sizeof (Idx));
  1107. return REG_NOERROR;
  1108. }
  1109. /* Copy into the top of DEST the items of SRC that are not
  1110. found in DEST. Maybe we could binary search in DEST? */
  1111. for (sbase = dest->nelem + 2 * src->nelem,
  1112. is = src->nelem - 1, id = dest->nelem - 1;
  1113. REG_VALID_INDEX (is) && REG_VALID_INDEX (id); )
  1114. {
  1115. if (dest->elems[id] == src->elems[is])
  1116. is--, id--;
  1117. else if (dest->elems[id] < src->elems[is])
  1118. dest->elems[--sbase] = src->elems[is--];
  1119. else /* if (dest->elems[id] > src->elems[is]) */
  1120. --id;
  1121. }
  1122. if (REG_VALID_INDEX (is))
  1123. {
  1124. /* If DEST is exhausted, the remaining items of SRC must be unique. */
  1125. sbase -= is + 1;
  1126. memcpy (dest->elems + sbase, src->elems, (is + 1) * sizeof (Idx));
  1127. }
  1128. id = dest->nelem - 1;
  1129. is = dest->nelem + 2 * src->nelem - 1;
  1130. delta = is - sbase + 1;
  1131. if (delta == 0)
  1132. return REG_NOERROR;
  1133. /* Now copy. When DELTA becomes zero, the remaining
  1134. DEST elements are already in place. */
  1135. dest->nelem += delta;
  1136. for (;;)
  1137. {
  1138. if (dest->elems[is] > dest->elems[id])
  1139. {
  1140. /* Copy from the top. */
  1141. dest->elems[id + delta--] = dest->elems[is--];
  1142. if (delta == 0)
  1143. break;
  1144. }
  1145. else
  1146. {
  1147. /* Slide from the bottom. */
  1148. dest->elems[id + delta] = dest->elems[id];
  1149. if (! REG_VALID_INDEX (--id))
  1150. {
  1151. /* Copy remaining SRC elements. */
  1152. memcpy (dest->elems, dest->elems + sbase,
  1153. delta * sizeof (Idx));
  1154. break;
  1155. }
  1156. }
  1157. }
  1158. return REG_NOERROR;
  1159. }
  1160. /* Insert the new element ELEM to the re_node_set* SET.
  1161. SET should not already have ELEM.
  1162. Return true if successful. */
  1163. static bool
  1164. internal_function
  1165. re_node_set_insert (re_node_set *set, Idx elem)
  1166. {
  1167. Idx idx;
  1168. /* In case the set is empty. */
  1169. if (set->alloc == 0)
  1170. return BE (re_node_set_init_1 (set, elem) == REG_NOERROR, 1);
  1171. if (BE (set->nelem, 0) == 0)
  1172. {
  1173. /* We already guaranteed above that set->alloc != 0. */
  1174. set->elems[0] = elem;
  1175. ++set->nelem;
  1176. return true;
  1177. }
  1178. /* Realloc if we need. */
  1179. if (set->alloc == set->nelem)
  1180. {
  1181. Idx *new_elems;
  1182. set->alloc = set->alloc * 2;
  1183. new_elems = re_realloc (set->elems, Idx, set->alloc);
  1184. if (BE (new_elems == NULL, 0))
  1185. return false;
  1186. set->elems = new_elems;
  1187. }
  1188. /* Move the elements which follows the new element. Test the
  1189. first element separately to skip a check in the inner loop. */
  1190. if (elem < set->elems[0])
  1191. {
  1192. idx = 0;
  1193. for (idx = set->nelem; idx > 0; idx--)
  1194. set->elems[idx] = set->elems[idx - 1];
  1195. }
  1196. else
  1197. {
  1198. for (idx = set->nelem; set->elems[idx - 1] > elem; idx--)
  1199. set->elems[idx] = set->elems[idx - 1];
  1200. }
  1201. /* Insert the new element. */
  1202. set->elems[idx] = elem;
  1203. ++set->nelem;
  1204. return true;
  1205. }
  1206. /* Insert the new element ELEM to the re_node_set* SET.
  1207. SET should not already have any element greater than or equal to ELEM.
  1208. Return true if successful. */
  1209. static bool
  1210. internal_function
  1211. re_node_set_insert_last (re_node_set *set, Idx elem)
  1212. {
  1213. /* Realloc if we need. */
  1214. if (set->alloc == set->nelem)
  1215. {
  1216. Idx *new_elems;
  1217. set->alloc = (set->alloc + 1) * 2;
  1218. new_elems = re_realloc (set->elems, Idx, set->alloc);
  1219. if (BE (new_elems == NULL, 0))
  1220. return false;
  1221. set->elems = new_elems;
  1222. }
  1223. /* Insert the new element. */
  1224. set->elems[set->nelem++] = elem;
  1225. return true;
  1226. }
  1227. /* Compare two node sets SET1 and SET2.
  1228. Return true if SET1 and SET2 are equivalent. */
  1229. static bool
  1230. internal_function __attribute ((pure))
  1231. re_node_set_compare (const re_node_set *set1, const re_node_set *set2)
  1232. {
  1233. Idx i;
  1234. if (set1 == NULL || set2 == NULL || set1->nelem != set2->nelem)
  1235. return false;
  1236. for (i = set1->nelem ; REG_VALID_INDEX (--i) ; )
  1237. if (set1->elems[i] != set2->elems[i])
  1238. return false;
  1239. return true;
  1240. }
  1241. /* Return (idx + 1) if SET contains the element ELEM, return 0 otherwise. */
  1242. static Idx
  1243. internal_function __attribute ((pure))
  1244. re_node_set_contains (const re_node_set *set, Idx elem)
  1245. {
  1246. __re_size_t idx, right, mid;
  1247. if (! REG_VALID_NONZERO_INDEX (set->nelem))
  1248. return 0;
  1249. /* Binary search the element. */
  1250. idx = 0;
  1251. right = set->nelem - 1;
  1252. while (idx < right)
  1253. {
  1254. mid = (idx + right) / 2;
  1255. if (set->elems[mid] < elem)
  1256. idx = mid + 1;
  1257. else
  1258. right = mid;
  1259. }
  1260. return set->elems[idx] == elem ? idx + 1 : 0;
  1261. }
  1262. static void
  1263. internal_function
  1264. re_node_set_remove_at (re_node_set *set, Idx idx)
  1265. {
  1266. if (idx < 0 || idx >= set->nelem)
  1267. return;
  1268. --set->nelem;
  1269. for (; idx < set->nelem; idx++)
  1270. set->elems[idx] = set->elems[idx + 1];
  1271. }
  1272. /* Add the token TOKEN to dfa->nodes, and return the index of the token.
  1273. Or return REG_MISSING if an error occurred. */
  1274. static Idx
  1275. internal_function
  1276. re_dfa_add_node (re_dfa_t *dfa, re_token_t token)
  1277. {
  1278. if (BE (dfa->nodes_len >= dfa->nodes_alloc, 0))
  1279. {
  1280. size_t new_nodes_alloc = dfa->nodes_alloc * 2;
  1281. Idx *new_nexts, *new_indices;
  1282. re_node_set *new_edests, *new_eclosures;
  1283. re_token_t *new_nodes;
  1284. size_t max_object_size =
  1285. MAX (sizeof (re_token_t),
  1286. MAX (sizeof (re_node_set),
  1287. sizeof (Idx)));
  1288. /* Avoid overflows. */
  1289. if (BE (SIZE_MAX / 2 / max_object_size < dfa->nodes_alloc, 0))
  1290. return REG_MISSING;
  1291. new_nodes = re_realloc (dfa->nodes, re_token_t, new_nodes_alloc);
  1292. if (BE (new_nodes == NULL, 0))
  1293. return REG_MISSING;
  1294. dfa->nodes = new_nodes;
  1295. new_nexts = re_realloc (dfa->nexts, Idx, new_nodes_alloc);
  1296. new_indices = re_realloc (dfa->org_indices, Idx, new_nodes_alloc);
  1297. new_edests = re_realloc (dfa->edests, re_node_set, new_nodes_alloc);
  1298. new_eclosures = re_realloc (dfa->eclosures, re_node_set, new_nodes_alloc);
  1299. if (BE (new_nexts == NULL || new_indices == NULL
  1300. || new_edests == NULL || new_eclosures == NULL, 0))
  1301. return REG_MISSING;
  1302. dfa->nexts = new_nexts;
  1303. dfa->org_indices = new_indices;
  1304. dfa->edests = new_edests;
  1305. dfa->eclosures = new_eclosures;
  1306. dfa->nodes_alloc = new_nodes_alloc;
  1307. }
  1308. dfa->nodes[dfa->nodes_len] = token;
  1309. dfa->nodes[dfa->nodes_len].constraint = 0;
  1310. #ifdef RE_ENABLE_I18N
  1311. {
  1312. int type = token.type;
  1313. dfa->nodes[dfa->nodes_len].accept_mb =
  1314. (type == OP_PERIOD && dfa->mb_cur_max > 1) || type == COMPLEX_BRACKET;
  1315. }
  1316. #endif
  1317. dfa->nexts[dfa->nodes_len] = REG_MISSING;
  1318. re_node_set_init_empty (dfa->edests + dfa->nodes_len);
  1319. re_node_set_init_empty (dfa->eclosures + dfa->nodes_len);
  1320. return dfa->nodes_len++;
  1321. }
  1322. static inline re_hashval_t
  1323. internal_function
  1324. calc_state_hash (const re_node_set *nodes, unsigned int context)
  1325. {
  1326. re_hashval_t hash = nodes->nelem + context;
  1327. Idx i;
  1328. for (i = 0 ; i < nodes->nelem ; i++)
  1329. hash += nodes->elems[i];
  1330. return hash;
  1331. }
  1332. /* Search for the state whose node_set is equivalent to NODES.
  1333. Return the pointer to the state, if we found it in the DFA.
  1334. Otherwise create the new one and return it. In case of an error
  1335. return NULL and set the error code in ERR.
  1336. Note: - We assume NULL as the invalid state, then it is possible that
  1337. return value is NULL and ERR is REG_NOERROR.
  1338. - We never return non-NULL value in case of any errors, it is for
  1339. optimization. */
  1340. static re_dfastate_t *
  1341. internal_function
  1342. re_acquire_state (reg_errcode_t *err, const re_dfa_t *dfa,
  1343. const re_node_set *nodes)
  1344. {
  1345. re_hashval_t hash;
  1346. re_dfastate_t *new_state;
  1347. struct re_state_table_entry *spot;
  1348. Idx i;
  1349. #ifdef lint
  1350. /* Suppress bogus uninitialized-variable warnings. */
  1351. *err = REG_NOERROR;
  1352. #endif
  1353. if (BE (nodes->nelem == 0, 0))
  1354. {
  1355. *err = REG_NOERROR;
  1356. return NULL;
  1357. }
  1358. hash = calc_state_hash (nodes, 0);
  1359. spot = dfa->state_table + (hash & dfa->state_hash_mask);
  1360. for (i = 0 ; i < spot->num ; i++)
  1361. {
  1362. re_dfastate_t *state = spot->array[i];
  1363. if (hash != state->hash)
  1364. continue;
  1365. if (re_node_set_compare (&state->nodes, nodes))
  1366. return state;
  1367. }
  1368. /* There are no appropriate state in the dfa, create the new one. */
  1369. new_state = create_ci_newstate (dfa, nodes, hash);
  1370. if (BE (new_state == NULL, 0))
  1371. *err = REG_ESPACE;
  1372. return new_state;
  1373. }
  1374. /* Search for the state whose node_set is equivalent to NODES and
  1375. whose context is equivalent to CONTEXT.
  1376. Return the pointer to the state, if we found it in the DFA.
  1377. Otherwise create the new one and return it. In case of an error
  1378. return NULL and set the error code in ERR.
  1379. Note: - We assume NULL as the invalid state, then it is possible that
  1380. return value is NULL and ERR is REG_NOERROR.
  1381. - We never return non-NULL value in case of any errors, it is for
  1382. optimization. */
  1383. static re_dfastate_t *
  1384. internal_function
  1385. re_acquire_state_context (reg_errcode_t *err, const re_dfa_t *dfa,
  1386. const re_node_set *nodes, unsigned int context)
  1387. {
  1388. re_hashval_t hash;
  1389. re_dfastate_t *new_state;
  1390. struct re_state_table_entry *spot;
  1391. Idx i;
  1392. #ifdef lint
  1393. /* Suppress bogus uninitialized-variable warnings. */
  1394. *err = REG_NOERROR;
  1395. #endif
  1396. if (nodes->nelem == 0)
  1397. {
  1398. *err = REG_NOERROR;
  1399. return NULL;
  1400. }
  1401. hash = calc_state_hash (nodes, context);
  1402. spot = dfa->state_table + (hash & dfa->state_hash_mask);
  1403. for (i = 0 ; i < spot->num ; i++)
  1404. {
  1405. re_dfastate_t *state = spot->array[i];
  1406. if (state->hash == hash
  1407. && state->context == context
  1408. && re_node_set_compare (state->entrance_nodes, nodes))
  1409. return state;
  1410. }
  1411. /* There are no appropriate state in `dfa', create the new one. */
  1412. new_state = create_cd_newstate (dfa, nodes, context, hash);
  1413. if (BE (new_state == NULL, 0))
  1414. *err = REG_ESPACE;
  1415. return new_state;
  1416. }
  1417. /* Finish initialization of the new state NEWSTATE, and using its hash value
  1418. HASH put in the appropriate bucket of DFA's state table. Return value
  1419. indicates the error code if failed. */
  1420. static reg_errcode_t
  1421. register_state (const re_dfa_t *dfa, re_dfastate_t *newstate,
  1422. re_hashval_t hash)
  1423. {
  1424. struct re_state_table_entry *spot;
  1425. reg_errcode_t err;
  1426. Idx i;
  1427. newstate->hash = hash;
  1428. err = re_node_set_alloc (&newstate->non_eps_nodes, newstate->nodes.nelem);
  1429. if (BE (err != REG_NOERROR, 0))
  1430. return REG_ESPACE;
  1431. for (i = 0; i < newstate->nodes.nelem; i++)
  1432. {
  1433. Idx elem = newstate->nodes.elems[i];
  1434. if (!IS_EPSILON_NODE (dfa->nodes[elem].type))
  1435. if (BE (! re_node_set_insert_last (&newstate->non_eps_nodes, elem), 0))
  1436. return REG_ESPACE;
  1437. }
  1438. spot = dfa->state_table + (hash & dfa->state_hash_mask);
  1439. if (BE (spot->alloc <= spot->num, 0))
  1440. {
  1441. Idx new_alloc = 2 * spot->num + 2;
  1442. re_dfastate_t **new_array = re_realloc (spot->array, re_dfastate_t *,
  1443. new_alloc);
  1444. if (BE (new_array == NULL, 0))
  1445. return REG_ESPACE;
  1446. spot->array = new_array;
  1447. spot->alloc = new_alloc;
  1448. }
  1449. spot->array[spot->num++] = newstate;
  1450. return REG_NOERROR;
  1451. }
  1452. static void
  1453. free_state (re_dfastate_t *state)
  1454. {
  1455. re_node_set_free (&state->non_eps_nodes);
  1456. re_node_set_free (&state->inveclosure);
  1457. if (state->entrance_nodes != &state->nodes)
  1458. {
  1459. re_node_set_free (state->entrance_nodes);
  1460. re_free (state->entrance_nodes);
  1461. }
  1462. re_node_set_free (&state->nodes);
  1463. re_free (state->word_trtable);
  1464. re_free (state->trtable);
  1465. re_free (state);
  1466. }
  1467. /* Create the new state which is independ of contexts.
  1468. Return the new state if succeeded, otherwise return NULL. */
  1469. static re_dfastate_t *
  1470. internal_function
  1471. create_ci_newstate (const re_dfa_t *dfa, const re_node_set *nodes,
  1472. re_hashval_t hash)
  1473. {
  1474. Idx i;
  1475. reg_errcode_t err;
  1476. re_dfastate_t *newstate;
  1477. newstate = (re_dfastate_t *) calloc (sizeof (re_dfastate_t), 1);
  1478. if (BE (newstate == NULL, 0))
  1479. return NULL;
  1480. err = re_node_set_init_copy (&newstate->nodes, nodes);
  1481. if (BE (err != REG_NOERROR, 0))
  1482. {
  1483. re_free (newstate);
  1484. return NULL;
  1485. }
  1486. newstate->entrance_nodes = &newstate->nodes;
  1487. for (i = 0 ; i < nodes->nelem ; i++)
  1488. {
  1489. re_token_t *node = dfa->nodes + nodes->elems[i];
  1490. re_token_type_t type = node->type;
  1491. if (type == CHARACTER && !node->constraint)
  1492. continue;
  1493. #ifdef RE_ENABLE_I18N
  1494. newstate->accept_mb |= node->accept_mb;
  1495. #endif /* RE_ENABLE_I18N */
  1496. /* If the state has the halt node, the state is a halt state. */
  1497. if (type == END_OF_RE)
  1498. newstate->halt = 1;
  1499. else if (type == OP_BACK_REF)
  1500. newstate->has_backref = 1;
  1501. else if (type == ANCHOR || node->constraint)
  1502. newstate->has_constraint = 1;
  1503. }
  1504. err = register_state (dfa, newstate, hash);
  1505. if (BE (err != REG_NOERROR, 0))
  1506. {
  1507. free_state (newstate);
  1508. newstate = NULL;
  1509. }
  1510. return newstate;
  1511. }
  1512. /* Create the new state which is depend on the context CONTEXT.
  1513. Return the new state if succeeded, otherwise return NULL. */
  1514. static re_dfastate_t *
  1515. internal_function
  1516. create_cd_newstate (const re_dfa_t *dfa, const re_node_set *nodes,
  1517. unsigned int context, re_hashval_t hash)
  1518. {
  1519. Idx i, nctx_nodes = 0;
  1520. reg_errcode_t err;
  1521. re_dfastate_t *newstate;
  1522. newstate = (re_dfastate_t *) calloc (sizeof (re_dfastate_t), 1);
  1523. if (BE (newstate == NULL, 0))
  1524. return NULL;
  1525. err = re_node_set_init_copy (&newstate->nodes, nodes);
  1526. if (BE (err != REG_NOERROR, 0))
  1527. {
  1528. re_free (newstate);
  1529. return NULL;
  1530. }
  1531. newstate->context = context;
  1532. newstate->entrance_nodes = &newstate->nodes;
  1533. for (i = 0 ; i < nodes->nelem ; i++)
  1534. {
  1535. re_token_t *node = dfa->nodes + nodes->elems[i];
  1536. re_token_type_t type = node->type;
  1537. unsigned int constraint = node->constraint;
  1538. if (type == CHARACTER && !constraint)
  1539. continue;
  1540. #ifdef RE_ENABLE_I18N
  1541. newstate->accept_mb |= node->accept_mb;
  1542. #endif /* RE_ENABLE_I18N */
  1543. /* If the state has the halt node, the state is a halt state. */
  1544. if (type == END_OF_RE)
  1545. newstate->halt = 1;
  1546. else if (type == OP_BACK_REF)
  1547. newstate->has_backref = 1;
  1548. if (constraint)
  1549. {
  1550. if (newstate->entrance_nodes == &newstate->nodes)
  1551. {
  1552. newstate->entrance_nodes = re_malloc (re_node_set, 1);
  1553. if (BE (newstate->entrance_nodes == NULL, 0))
  1554. {
  1555. free_state (newstate);
  1556. return NULL;
  1557. }
  1558. re_node_set_init_copy (newstate->entrance_nodes, nodes);
  1559. nctx_nodes = 0;
  1560. newstate->has_constraint = 1;
  1561. }
  1562. if (NOT_SATISFY_PREV_CONSTRAINT (constraint,context))
  1563. {
  1564. re_node_set_remove_at (&newstate->nodes, i - nctx_nodes);
  1565. ++nctx_nodes;
  1566. }
  1567. }
  1568. }
  1569. err = register_state (dfa, newstate, hash);
  1570. if (BE (err != REG_NOERROR, 0))
  1571. {
  1572. free_state (newstate);
  1573. newstate = NULL;
  1574. }
  1575. return newstate;
  1576. }