regex_internal.c 47 KB

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