regex_internal.c 47 KB

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