regex_internal.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741
  1. /* Extended regular expression matching and search library.
  2. Copyright (C) 2002-2013 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 General Public
  7. License as published by the Free Software Foundation; either
  8. version 3 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. General Public License for more details.
  13. You should have received a copy of the GNU General Public
  14. License along with the GNU C Library; if not, see
  15. <http://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) 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 in realloc. */
  120. const size_t max_object_size = MAX (sizeof (wint_t), sizeof (Idx));
  121. if (BE (MIN (IDX_MAX, 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) -1 || mbclen == 0
  212. || (mbclen == (size_t) -2 && pstr->bufs_len >= pstr->len), 0))
  213. {
  214. /* We treat these cases as a singlebyte character. */
  215. mbclen = 1;
  216. wc = (wchar_t) pstr->raw_mbs[pstr->raw_mbs_idx + byte_idx];
  217. if (BE (pstr->trans != NULL, 0))
  218. wc = pstr->trans[wc];
  219. pstr->cur_state = prev_st;
  220. }
  221. else if (BE (mbclen == (size_t) -2, 0))
  222. {
  223. /* The buffer doesn't have enough space, finish to build. */
  224. pstr->cur_state = prev_st;
  225. break;
  226. }
  227. /* Write wide character and padding. */
  228. pstr->wcs[byte_idx++] = wc;
  229. /* Write paddings. */
  230. for (remain_len = byte_idx + mbclen - 1; byte_idx < remain_len ;)
  231. pstr->wcs[byte_idx++] = WEOF;
  232. }
  233. pstr->valid_len = byte_idx;
  234. pstr->valid_raw_len = byte_idx;
  235. }
  236. /* Build wide character buffer PSTR->WCS like build_wcs_buffer,
  237. but for REG_ICASE. */
  238. static reg_errcode_t
  239. internal_function __attribute_warn_unused_result__
  240. build_wcs_upper_buffer (re_string_t *pstr)
  241. {
  242. mbstate_t prev_st;
  243. Idx src_idx, byte_idx, end_idx, remain_len;
  244. size_t mbclen;
  245. #ifdef _LIBC
  246. char buf[MB_LEN_MAX];
  247. assert (MB_LEN_MAX >= pstr->mb_cur_max);
  248. #else
  249. char buf[64];
  250. #endif
  251. byte_idx = pstr->valid_len;
  252. end_idx = (pstr->bufs_len > pstr->len) ? pstr->len : pstr->bufs_len;
  253. /* The following optimization assumes that ASCII characters can be
  254. mapped to wide characters with a simple cast. */
  255. if (! pstr->map_notascii && pstr->trans == NULL && !pstr->offsets_needed)
  256. {
  257. while (byte_idx < end_idx)
  258. {
  259. wchar_t wc;
  260. if (isascii (pstr->raw_mbs[pstr->raw_mbs_idx + byte_idx])
  261. && mbsinit (&pstr->cur_state))
  262. {
  263. /* In case of a singlebyte character. */
  264. pstr->mbs[byte_idx]
  265. = toupper (pstr->raw_mbs[pstr->raw_mbs_idx + byte_idx]);
  266. /* The next step uses the assumption that wchar_t is encoded
  267. ASCII-safe: all ASCII values can be converted like this. */
  268. pstr->wcs[byte_idx] = (wchar_t) pstr->mbs[byte_idx];
  269. ++byte_idx;
  270. continue;
  271. }
  272. remain_len = end_idx - byte_idx;
  273. prev_st = pstr->cur_state;
  274. mbclen = __mbrtowc (&wc,
  275. ((const char *) pstr->raw_mbs + pstr->raw_mbs_idx
  276. + byte_idx), remain_len, &pstr->cur_state);
  277. if (BE (mbclen < (size_t) -2, 1))
  278. {
  279. wchar_t wcu = wc;
  280. if (iswlower (wc))
  281. {
  282. size_t mbcdlen;
  283. wcu = towupper (wc);
  284. mbcdlen = wcrtomb (buf, wcu, &prev_st);
  285. if (BE (mbclen == mbcdlen, 1))
  286. memcpy (pstr->mbs + byte_idx, buf, mbclen);
  287. else
  288. {
  289. src_idx = byte_idx;
  290. goto offsets_needed;
  291. }
  292. }
  293. else
  294. memcpy (pstr->mbs + byte_idx,
  295. pstr->raw_mbs + pstr->raw_mbs_idx + byte_idx, mbclen);
  296. pstr->wcs[byte_idx++] = wcu;
  297. /* Write paddings. */
  298. for (remain_len = byte_idx + mbclen - 1; byte_idx < remain_len ;)
  299. pstr->wcs[byte_idx++] = WEOF;
  300. }
  301. else if (mbclen == (size_t) -1 || mbclen == 0
  302. || (mbclen == (size_t) -2 && pstr->bufs_len >= pstr->len))
  303. {
  304. /* It is an invalid character, an incomplete character
  305. at the end of the string, or '\0'. Just use the byte. */
  306. int ch = pstr->raw_mbs[pstr->raw_mbs_idx + byte_idx];
  307. pstr->mbs[byte_idx] = ch;
  308. /* And also cast it to wide char. */
  309. pstr->wcs[byte_idx++] = (wchar_t) ch;
  310. if (BE (mbclen == (size_t) -1, 0))
  311. pstr->cur_state = prev_st;
  312. }
  313. else
  314. {
  315. /* The buffer doesn't have enough space, finish to build. */
  316. pstr->cur_state = prev_st;
  317. break;
  318. }
  319. }
  320. pstr->valid_len = byte_idx;
  321. pstr->valid_raw_len = byte_idx;
  322. return REG_NOERROR;
  323. }
  324. else
  325. for (src_idx = pstr->valid_raw_len; byte_idx < end_idx;)
  326. {
  327. wchar_t wc;
  328. const char *p;
  329. offsets_needed:
  330. remain_len = end_idx - byte_idx;
  331. prev_st = pstr->cur_state;
  332. if (BE (pstr->trans != NULL, 0))
  333. {
  334. int i, ch;
  335. for (i = 0; i < pstr->mb_cur_max && i < remain_len; ++i)
  336. {
  337. ch = pstr->raw_mbs [pstr->raw_mbs_idx + src_idx + i];
  338. buf[i] = pstr->trans[ch];
  339. }
  340. p = (const char *) buf;
  341. }
  342. else
  343. p = (const char *) pstr->raw_mbs + pstr->raw_mbs_idx + src_idx;
  344. mbclen = __mbrtowc (&wc, p, remain_len, &pstr->cur_state);
  345. if (BE (mbclen < (size_t) -2, 1))
  346. {
  347. wchar_t wcu = wc;
  348. if (iswlower (wc))
  349. {
  350. size_t mbcdlen;
  351. wcu = towupper (wc);
  352. mbcdlen = wcrtomb ((char *) buf, wcu, &prev_st);
  353. if (BE (mbclen == mbcdlen, 1))
  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 (BE (pstr->offsets_needed != 0, 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 (BE (pstr->trans != NULL, 0))
  416. ch = pstr->trans [ch];
  417. pstr->mbs[byte_idx] = ch;
  418. if (BE (pstr->offsets_needed != 0, 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 (BE (mbclen == (size_t) -1, 0))
  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. internal_function
  441. re_string_skip_chars (re_string_t *pstr, Idx new_raw_idx, wint_t *last_wc)
  442. {
  443. mbstate_t prev_st;
  444. Idx rawbuf_idx;
  445. size_t mbclen;
  446. wint_t wc = WEOF;
  447. /* Skip the characters which are not necessary to check. */
  448. for (rawbuf_idx = pstr->raw_mbs_idx + pstr->valid_raw_len;
  449. rawbuf_idx < new_raw_idx;)
  450. {
  451. wchar_t wc2;
  452. Idx remain_len = pstr->raw_len - rawbuf_idx;
  453. prev_st = pstr->cur_state;
  454. mbclen = __mbrtowc (&wc2, (const char *) pstr->raw_mbs + rawbuf_idx,
  455. remain_len, &pstr->cur_state);
  456. if (BE (mbclen == (size_t) -2 || mbclen == (size_t) -1 || mbclen == 0, 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. #endif /* RE_ENABLE_I18N */
  475. /* Build the buffer PSTR->MBS, and apply the translation if we need.
  476. This function is used in case of REG_ICASE. */
  477. static void
  478. internal_function
  479. build_upper_buffer (re_string_t *pstr)
  480. {
  481. Idx char_idx, end_idx;
  482. end_idx = (pstr->bufs_len > pstr->len) ? pstr->len : pstr->bufs_len;
  483. for (char_idx = pstr->valid_len; char_idx < end_idx; ++char_idx)
  484. {
  485. int ch = pstr->raw_mbs[pstr->raw_mbs_idx + char_idx];
  486. if (BE (pstr->trans != NULL, 0))
  487. ch = pstr->trans[ch];
  488. if (islower (ch))
  489. pstr->mbs[char_idx] = toupper (ch);
  490. else
  491. pstr->mbs[char_idx] = ch;
  492. }
  493. pstr->valid_len = char_idx;
  494. pstr->valid_raw_len = char_idx;
  495. }
  496. /* Apply TRANS to the buffer in PSTR. */
  497. static void
  498. internal_function
  499. re_string_translate_buffer (re_string_t *pstr)
  500. {
  501. Idx buf_idx, end_idx;
  502. end_idx = (pstr->bufs_len > pstr->len) ? pstr->len : pstr->bufs_len;
  503. for (buf_idx = pstr->valid_len; buf_idx < end_idx; ++buf_idx)
  504. {
  505. int ch = pstr->raw_mbs[pstr->raw_mbs_idx + buf_idx];
  506. pstr->mbs[buf_idx] = pstr->trans[ch];
  507. }
  508. pstr->valid_len = buf_idx;
  509. pstr->valid_raw_len = buf_idx;
  510. }
  511. /* This function re-construct the buffers.
  512. Concretely, convert to wide character in case of pstr->mb_cur_max > 1,
  513. convert to upper case in case of REG_ICASE, apply translation. */
  514. static reg_errcode_t
  515. internal_function __attribute_warn_unused_result__
  516. re_string_reconstruct (re_string_t *pstr, Idx idx, int eflags)
  517. {
  518. Idx offset;
  519. if (BE (pstr->raw_mbs_idx <= idx, 0))
  520. offset = idx - pstr->raw_mbs_idx;
  521. else
  522. {
  523. /* Reset buffer. */
  524. #ifdef RE_ENABLE_I18N
  525. if (pstr->mb_cur_max > 1)
  526. memset (&pstr->cur_state, '\0', sizeof (mbstate_t));
  527. #endif /* RE_ENABLE_I18N */
  528. pstr->len = pstr->raw_len;
  529. pstr->stop = pstr->raw_stop;
  530. pstr->valid_len = 0;
  531. pstr->raw_mbs_idx = 0;
  532. pstr->valid_raw_len = 0;
  533. pstr->offsets_needed = 0;
  534. pstr->tip_context = ((eflags & REG_NOTBOL) ? CONTEXT_BEGBUF
  535. : CONTEXT_NEWLINE | CONTEXT_BEGBUF);
  536. if (!pstr->mbs_allocated)
  537. pstr->mbs = (unsigned char *) pstr->raw_mbs;
  538. offset = idx;
  539. }
  540. if (BE (offset != 0, 1))
  541. {
  542. /* Should the already checked characters be kept? */
  543. if (BE (offset < pstr->valid_raw_len, 1))
  544. {
  545. /* Yes, move them to the front of the buffer. */
  546. #ifdef RE_ENABLE_I18N
  547. if (BE (pstr->offsets_needed, 0))
  548. {
  549. Idx low = 0, high = pstr->valid_len, mid;
  550. do
  551. {
  552. mid = (high + low) / 2;
  553. if (pstr->offsets[mid] > offset)
  554. high = mid;
  555. else if (pstr->offsets[mid] < offset)
  556. low = mid + 1;
  557. else
  558. break;
  559. }
  560. while (low < high);
  561. if (pstr->offsets[mid] < offset)
  562. ++mid;
  563. pstr->tip_context = re_string_context_at (pstr, mid - 1,
  564. eflags);
  565. /* This can be quite complicated, so handle specially
  566. only the common and easy case where the character with
  567. different length representation of lower and upper
  568. case is present at or after offset. */
  569. if (pstr->valid_len > offset
  570. && mid == offset && pstr->offsets[mid] == offset)
  571. {
  572. memmove (pstr->wcs, pstr->wcs + offset,
  573. (pstr->valid_len - offset) * sizeof (wint_t));
  574. memmove (pstr->mbs, pstr->mbs + offset, pstr->valid_len - offset);
  575. pstr->valid_len -= offset;
  576. pstr->valid_raw_len -= offset;
  577. for (low = 0; low < pstr->valid_len; low++)
  578. pstr->offsets[low] = pstr->offsets[low + offset] - offset;
  579. }
  580. else
  581. {
  582. /* Otherwise, just find out how long the partial multibyte
  583. character at offset is and fill it with WEOF/255. */
  584. pstr->len = pstr->raw_len - idx + offset;
  585. pstr->stop = pstr->raw_stop - idx + offset;
  586. pstr->offsets_needed = 0;
  587. while (mid > 0 && pstr->offsets[mid - 1] == offset)
  588. --mid;
  589. while (mid < pstr->valid_len)
  590. if (pstr->wcs[mid] != WEOF)
  591. break;
  592. else
  593. ++mid;
  594. if (mid == pstr->valid_len)
  595. pstr->valid_len = 0;
  596. else
  597. {
  598. pstr->valid_len = pstr->offsets[mid] - offset;
  599. if (pstr->valid_len)
  600. {
  601. for (low = 0; low < pstr->valid_len; ++low)
  602. pstr->wcs[low] = WEOF;
  603. memset (pstr->mbs, 255, pstr->valid_len);
  604. }
  605. }
  606. pstr->valid_raw_len = pstr->valid_len;
  607. }
  608. }
  609. else
  610. #endif
  611. {
  612. pstr->tip_context = re_string_context_at (pstr, offset - 1,
  613. eflags);
  614. #ifdef RE_ENABLE_I18N
  615. if (pstr->mb_cur_max > 1)
  616. memmove (pstr->wcs, pstr->wcs + offset,
  617. (pstr->valid_len - offset) * sizeof (wint_t));
  618. #endif /* RE_ENABLE_I18N */
  619. if (BE (pstr->mbs_allocated, 0))
  620. memmove (pstr->mbs, pstr->mbs + offset,
  621. pstr->valid_len - offset);
  622. pstr->valid_len -= offset;
  623. pstr->valid_raw_len -= offset;
  624. #if DEBUG
  625. assert (pstr->valid_len > 0);
  626. #endif
  627. }
  628. }
  629. else
  630. {
  631. #ifdef RE_ENABLE_I18N
  632. /* No, skip all characters until IDX. */
  633. Idx prev_valid_len = pstr->valid_len;
  634. if (BE (pstr->offsets_needed, 0))
  635. {
  636. pstr->len = pstr->raw_len - idx + offset;
  637. pstr->stop = pstr->raw_stop - idx + offset;
  638. pstr->offsets_needed = 0;
  639. }
  640. #endif
  641. pstr->valid_len = 0;
  642. #ifdef RE_ENABLE_I18N
  643. if (pstr->mb_cur_max > 1)
  644. {
  645. Idx wcs_idx;
  646. wint_t wc = WEOF;
  647. if (pstr->is_utf8)
  648. {
  649. const unsigned char *raw, *p, *end;
  650. /* Special case UTF-8. Multi-byte chars start with any
  651. byte other than 0x80 - 0xbf. */
  652. raw = pstr->raw_mbs + pstr->raw_mbs_idx;
  653. end = raw + (offset - pstr->mb_cur_max);
  654. if (end < pstr->raw_mbs)
  655. end = pstr->raw_mbs;
  656. p = raw + offset - 1;
  657. #ifdef _LIBC
  658. /* We know the wchar_t encoding is UCS4, so for the simple
  659. case, ASCII characters, skip the conversion step. */
  660. if (isascii (*p) && BE (pstr->trans == NULL, 1))
  661. {
  662. memset (&pstr->cur_state, '\0', sizeof (mbstate_t));
  663. /* pstr->valid_len = 0; */
  664. wc = (wchar_t) *p;
  665. }
  666. else
  667. #endif
  668. for (; p >= end; --p)
  669. if ((*p & 0xc0) != 0x80)
  670. {
  671. mbstate_t cur_state;
  672. wchar_t wc2;
  673. Idx mlen = raw + pstr->len - p;
  674. unsigned char buf[6];
  675. size_t mbclen;
  676. const unsigned char *pp = p;
  677. if (BE (pstr->trans != NULL, 0))
  678. {
  679. int i = mlen < 6 ? mlen : 6;
  680. while (--i >= 0)
  681. buf[i] = pstr->trans[p[i]];
  682. pp = buf;
  683. }
  684. /* XXX Don't use mbrtowc, we know which conversion
  685. to use (UTF-8 -> UCS4). */
  686. memset (&cur_state, 0, sizeof (cur_state));
  687. mbclen = __mbrtowc (&wc2, (const char *) pp, mlen,
  688. &cur_state);
  689. if (raw + offset - p <= mbclen
  690. && mbclen < (size_t) -2)
  691. {
  692. memset (&pstr->cur_state, '\0',
  693. sizeof (mbstate_t));
  694. pstr->valid_len = mbclen - (raw + offset - p);
  695. wc = wc2;
  696. }
  697. break;
  698. }
  699. }
  700. if (wc == WEOF)
  701. pstr->valid_len = re_string_skip_chars (pstr, idx, &wc) - idx;
  702. if (wc == WEOF)
  703. pstr->tip_context
  704. = re_string_context_at (pstr, prev_valid_len - 1, eflags);
  705. else
  706. pstr->tip_context = ((BE (pstr->word_ops_used != 0, 0)
  707. && IS_WIDE_WORD_CHAR (wc))
  708. ? CONTEXT_WORD
  709. : ((IS_WIDE_NEWLINE (wc)
  710. && pstr->newline_anchor)
  711. ? CONTEXT_NEWLINE : 0));
  712. if (BE (pstr->valid_len, 0))
  713. {
  714. for (wcs_idx = 0; wcs_idx < pstr->valid_len; ++wcs_idx)
  715. pstr->wcs[wcs_idx] = WEOF;
  716. if (pstr->mbs_allocated)
  717. memset (pstr->mbs, 255, pstr->valid_len);
  718. }
  719. pstr->valid_raw_len = pstr->valid_len;
  720. }
  721. else
  722. #endif /* RE_ENABLE_I18N */
  723. {
  724. int c = pstr->raw_mbs[pstr->raw_mbs_idx + offset - 1];
  725. pstr->valid_raw_len = 0;
  726. if (pstr->trans)
  727. c = pstr->trans[c];
  728. pstr->tip_context = (bitset_contain (pstr->word_char, c)
  729. ? CONTEXT_WORD
  730. : ((IS_NEWLINE (c) && pstr->newline_anchor)
  731. ? CONTEXT_NEWLINE : 0));
  732. }
  733. }
  734. if (!BE (pstr->mbs_allocated, 0))
  735. pstr->mbs += offset;
  736. }
  737. pstr->raw_mbs_idx = idx;
  738. pstr->len -= offset;
  739. pstr->stop -= offset;
  740. /* Then build the buffers. */
  741. #ifdef RE_ENABLE_I18N
  742. if (pstr->mb_cur_max > 1)
  743. {
  744. if (pstr->icase)
  745. {
  746. reg_errcode_t ret = build_wcs_upper_buffer (pstr);
  747. if (BE (ret != REG_NOERROR, 0))
  748. return ret;
  749. }
  750. else
  751. build_wcs_buffer (pstr);
  752. }
  753. else
  754. #endif /* RE_ENABLE_I18N */
  755. if (BE (pstr->mbs_allocated, 0))
  756. {
  757. if (pstr->icase)
  758. build_upper_buffer (pstr);
  759. else if (pstr->trans != NULL)
  760. re_string_translate_buffer (pstr);
  761. }
  762. else
  763. pstr->valid_len = pstr->len;
  764. pstr->cur_idx = 0;
  765. return REG_NOERROR;
  766. }
  767. static unsigned char
  768. internal_function __attribute__ ((pure))
  769. re_string_peek_byte_case (const re_string_t *pstr, Idx idx)
  770. {
  771. int ch;
  772. Idx off;
  773. /* Handle the common (easiest) cases first. */
  774. if (BE (!pstr->mbs_allocated, 1))
  775. return re_string_peek_byte (pstr, idx);
  776. #ifdef RE_ENABLE_I18N
  777. if (pstr->mb_cur_max > 1
  778. && ! re_string_is_single_byte_char (pstr, pstr->cur_idx + idx))
  779. return re_string_peek_byte (pstr, idx);
  780. #endif
  781. off = pstr->cur_idx + idx;
  782. #ifdef RE_ENABLE_I18N
  783. if (pstr->offsets_needed)
  784. off = pstr->offsets[off];
  785. #endif
  786. ch = pstr->raw_mbs[pstr->raw_mbs_idx + off];
  787. #ifdef RE_ENABLE_I18N
  788. /* Ensure that e.g. for tr_TR.UTF-8 BACKSLASH DOTLESS SMALL LETTER I
  789. this function returns CAPITAL LETTER I instead of first byte of
  790. DOTLESS SMALL LETTER I. The latter would confuse the parser,
  791. since peek_byte_case doesn't advance cur_idx in any way. */
  792. if (pstr->offsets_needed && !isascii (ch))
  793. return re_string_peek_byte (pstr, idx);
  794. #endif
  795. return ch;
  796. }
  797. static unsigned char
  798. internal_function
  799. re_string_fetch_byte_case (re_string_t *pstr)
  800. {
  801. if (BE (!pstr->mbs_allocated, 1))
  802. return re_string_fetch_byte (pstr);
  803. #ifdef RE_ENABLE_I18N
  804. if (pstr->offsets_needed)
  805. {
  806. Idx off;
  807. int ch;
  808. /* For tr_TR.UTF-8 [[:islower:]] there is
  809. [[: CAPITAL LETTER I WITH DOT lower:]] in mbs. Skip
  810. in that case the whole multi-byte character and return
  811. the original letter. On the other side, with
  812. [[: DOTLESS SMALL LETTER I return [[:I, as doing
  813. anything else would complicate things too much. */
  814. if (!re_string_first_byte (pstr, pstr->cur_idx))
  815. return re_string_fetch_byte (pstr);
  816. off = pstr->offsets[pstr->cur_idx];
  817. ch = pstr->raw_mbs[pstr->raw_mbs_idx + off];
  818. if (! isascii (ch))
  819. return re_string_fetch_byte (pstr);
  820. re_string_skip_bytes (pstr,
  821. re_string_char_size_at (pstr, pstr->cur_idx));
  822. return ch;
  823. }
  824. #endif
  825. return pstr->raw_mbs[pstr->raw_mbs_idx + pstr->cur_idx++];
  826. }
  827. static void
  828. internal_function
  829. re_string_destruct (re_string_t *pstr)
  830. {
  831. #ifdef RE_ENABLE_I18N
  832. re_free (pstr->wcs);
  833. re_free (pstr->offsets);
  834. #endif /* RE_ENABLE_I18N */
  835. if (pstr->mbs_allocated)
  836. re_free (pstr->mbs);
  837. }
  838. /* Return the context at IDX in INPUT. */
  839. static unsigned int
  840. internal_function
  841. re_string_context_at (const re_string_t *input, Idx idx, int eflags)
  842. {
  843. int c;
  844. if (BE (! REG_VALID_INDEX (idx), 0))
  845. /* In this case, we use the value stored in input->tip_context,
  846. since we can't know the character in input->mbs[-1] here. */
  847. return input->tip_context;
  848. if (BE (idx == input->len, 0))
  849. return ((eflags & REG_NOTEOL) ? CONTEXT_ENDBUF
  850. : CONTEXT_NEWLINE | CONTEXT_ENDBUF);
  851. #ifdef RE_ENABLE_I18N
  852. if (input->mb_cur_max > 1)
  853. {
  854. wint_t wc;
  855. Idx wc_idx = idx;
  856. while(input->wcs[wc_idx] == WEOF)
  857. {
  858. #ifdef DEBUG
  859. /* It must not happen. */
  860. assert (REG_VALID_INDEX (wc_idx));
  861. #endif
  862. --wc_idx;
  863. if (! REG_VALID_INDEX (wc_idx))
  864. return input->tip_context;
  865. }
  866. wc = input->wcs[wc_idx];
  867. if (BE (input->word_ops_used != 0, 0) && IS_WIDE_WORD_CHAR (wc))
  868. return CONTEXT_WORD;
  869. return (IS_WIDE_NEWLINE (wc) && input->newline_anchor
  870. ? CONTEXT_NEWLINE : 0);
  871. }
  872. else
  873. #endif
  874. {
  875. c = re_string_byte_at (input, idx);
  876. if (bitset_contain (input->word_char, c))
  877. return CONTEXT_WORD;
  878. return IS_NEWLINE (c) && input->newline_anchor ? CONTEXT_NEWLINE : 0;
  879. }
  880. }
  881. /* Functions for set operation. */
  882. static reg_errcode_t
  883. internal_function __attribute_warn_unused_result__
  884. re_node_set_alloc (re_node_set *set, Idx size)
  885. {
  886. set->alloc = size;
  887. set->nelem = 0;
  888. set->elems = re_malloc (Idx, size);
  889. if (BE (set->elems == NULL, 0) && (MALLOC_0_IS_NONNULL || size != 0))
  890. return REG_ESPACE;
  891. return REG_NOERROR;
  892. }
  893. static reg_errcode_t
  894. internal_function __attribute_warn_unused_result__
  895. re_node_set_init_1 (re_node_set *set, Idx elem)
  896. {
  897. set->alloc = 1;
  898. set->nelem = 1;
  899. set->elems = re_malloc (Idx, 1);
  900. if (BE (set->elems == NULL, 0))
  901. {
  902. set->alloc = set->nelem = 0;
  903. return REG_ESPACE;
  904. }
  905. set->elems[0] = elem;
  906. return REG_NOERROR;
  907. }
  908. static reg_errcode_t
  909. internal_function __attribute_warn_unused_result__
  910. re_node_set_init_2 (re_node_set *set, Idx elem1, Idx elem2)
  911. {
  912. set->alloc = 2;
  913. set->elems = re_malloc (Idx, 2);
  914. if (BE (set->elems == NULL, 0))
  915. return REG_ESPACE;
  916. if (elem1 == elem2)
  917. {
  918. set->nelem = 1;
  919. set->elems[0] = elem1;
  920. }
  921. else
  922. {
  923. set->nelem = 2;
  924. if (elem1 < elem2)
  925. {
  926. set->elems[0] = elem1;
  927. set->elems[1] = elem2;
  928. }
  929. else
  930. {
  931. set->elems[0] = elem2;
  932. set->elems[1] = elem1;
  933. }
  934. }
  935. return REG_NOERROR;
  936. }
  937. static reg_errcode_t
  938. internal_function __attribute_warn_unused_result__
  939. re_node_set_init_copy (re_node_set *dest, const re_node_set *src)
  940. {
  941. dest->nelem = src->nelem;
  942. if (src->nelem > 0)
  943. {
  944. dest->alloc = dest->nelem;
  945. dest->elems = re_malloc (Idx, dest->alloc);
  946. if (BE (dest->elems == NULL, 0))
  947. {
  948. dest->alloc = dest->nelem = 0;
  949. return REG_ESPACE;
  950. }
  951. memcpy (dest->elems, src->elems, src->nelem * sizeof (Idx));
  952. }
  953. else
  954. re_node_set_init_empty (dest);
  955. return REG_NOERROR;
  956. }
  957. /* Calculate the intersection of the sets SRC1 and SRC2. And merge it to
  958. DEST. Return value indicate the error code or REG_NOERROR if succeeded.
  959. Note: We assume dest->elems is NULL, when dest->alloc is 0. */
  960. static reg_errcode_t
  961. internal_function __attribute_warn_unused_result__
  962. re_node_set_add_intersect (re_node_set *dest, const re_node_set *src1,
  963. const re_node_set *src2)
  964. {
  965. Idx i1, i2, is, id, delta, sbase;
  966. if (src1->nelem == 0 || src2->nelem == 0)
  967. return REG_NOERROR;
  968. /* We need dest->nelem + 2 * elems_in_intersection; this is a
  969. conservative estimate. */
  970. if (src1->nelem + src2->nelem + dest->nelem > dest->alloc)
  971. {
  972. Idx new_alloc = src1->nelem + src2->nelem + dest->alloc;
  973. Idx *new_elems = re_realloc (dest->elems, Idx, new_alloc);
  974. if (BE (new_elems == NULL, 0))
  975. return REG_ESPACE;
  976. dest->elems = new_elems;
  977. dest->alloc = new_alloc;
  978. }
  979. /* Find the items in the intersection of SRC1 and SRC2, and copy
  980. into the top of DEST those that are not already in DEST itself. */
  981. sbase = dest->nelem + src1->nelem + src2->nelem;
  982. i1 = src1->nelem - 1;
  983. i2 = src2->nelem - 1;
  984. id = dest->nelem - 1;
  985. for (;;)
  986. {
  987. if (src1->elems[i1] == src2->elems[i2])
  988. {
  989. /* Try to find the item in DEST. Maybe we could binary search? */
  990. while (REG_VALID_INDEX (id) && dest->elems[id] > src1->elems[i1])
  991. --id;
  992. if (! REG_VALID_INDEX (id) || dest->elems[id] != src1->elems[i1])
  993. dest->elems[--sbase] = src1->elems[i1];
  994. if (! REG_VALID_INDEX (--i1) || ! REG_VALID_INDEX (--i2))
  995. break;
  996. }
  997. /* Lower the highest of the two items. */
  998. else if (src1->elems[i1] < src2->elems[i2])
  999. {
  1000. if (! REG_VALID_INDEX (--i2))
  1001. break;
  1002. }
  1003. else
  1004. {
  1005. if (! REG_VALID_INDEX (--i1))
  1006. break;
  1007. }
  1008. }
  1009. id = dest->nelem - 1;
  1010. is = dest->nelem + src1->nelem + src2->nelem - 1;
  1011. delta = is - sbase + 1;
  1012. /* Now copy. When DELTA becomes zero, the remaining
  1013. DEST elements are already in place; this is more or
  1014. less the same loop that is in re_node_set_merge. */
  1015. dest->nelem += delta;
  1016. if (delta > 0 && REG_VALID_INDEX (id))
  1017. for (;;)
  1018. {
  1019. if (dest->elems[is] > dest->elems[id])
  1020. {
  1021. /* Copy from the top. */
  1022. dest->elems[id + delta--] = dest->elems[is--];
  1023. if (delta == 0)
  1024. break;
  1025. }
  1026. else
  1027. {
  1028. /* Slide from the bottom. */
  1029. dest->elems[id + delta] = dest->elems[id];
  1030. if (! REG_VALID_INDEX (--id))
  1031. break;
  1032. }
  1033. }
  1034. /* Copy remaining SRC elements. */
  1035. memcpy (dest->elems, dest->elems + sbase, delta * sizeof (Idx));
  1036. return REG_NOERROR;
  1037. }
  1038. /* Calculate the union set of the sets SRC1 and SRC2. And store it to
  1039. DEST. Return value indicate the error code or REG_NOERROR if succeeded. */
  1040. static reg_errcode_t
  1041. internal_function __attribute_warn_unused_result__
  1042. re_node_set_init_union (re_node_set *dest, const re_node_set *src1,
  1043. const re_node_set *src2)
  1044. {
  1045. Idx i1, i2, id;
  1046. if (src1 != NULL && src1->nelem > 0 && src2 != NULL && src2->nelem > 0)
  1047. {
  1048. dest->alloc = src1->nelem + src2->nelem;
  1049. dest->elems = re_malloc (Idx, dest->alloc);
  1050. if (BE (dest->elems == NULL, 0))
  1051. return REG_ESPACE;
  1052. }
  1053. else
  1054. {
  1055. if (src1 != NULL && src1->nelem > 0)
  1056. return re_node_set_init_copy (dest, src1);
  1057. else if (src2 != NULL && src2->nelem > 0)
  1058. return re_node_set_init_copy (dest, src2);
  1059. else
  1060. re_node_set_init_empty (dest);
  1061. return REG_NOERROR;
  1062. }
  1063. for (i1 = i2 = id = 0 ; i1 < src1->nelem && i2 < src2->nelem ;)
  1064. {
  1065. if (src1->elems[i1] > src2->elems[i2])
  1066. {
  1067. dest->elems[id++] = src2->elems[i2++];
  1068. continue;
  1069. }
  1070. if (src1->elems[i1] == src2->elems[i2])
  1071. ++i2;
  1072. dest->elems[id++] = src1->elems[i1++];
  1073. }
  1074. if (i1 < src1->nelem)
  1075. {
  1076. memcpy (dest->elems + id, src1->elems + i1,
  1077. (src1->nelem - i1) * sizeof (Idx));
  1078. id += src1->nelem - i1;
  1079. }
  1080. else if (i2 < src2->nelem)
  1081. {
  1082. memcpy (dest->elems + id, src2->elems + i2,
  1083. (src2->nelem - i2) * sizeof (Idx));
  1084. id += src2->nelem - i2;
  1085. }
  1086. dest->nelem = id;
  1087. return REG_NOERROR;
  1088. }
  1089. /* Calculate the union set of the sets DEST and SRC. And store it to
  1090. DEST. Return value indicate the error code or REG_NOERROR if succeeded. */
  1091. static reg_errcode_t
  1092. internal_function __attribute_warn_unused_result__
  1093. re_node_set_merge (re_node_set *dest, const re_node_set *src)
  1094. {
  1095. Idx is, id, sbase, delta;
  1096. if (src == NULL || src->nelem == 0)
  1097. return REG_NOERROR;
  1098. if (dest->alloc < 2 * src->nelem + dest->nelem)
  1099. {
  1100. Idx new_alloc = 2 * (src->nelem + dest->alloc);
  1101. Idx *new_buffer = re_realloc (dest->elems, Idx, new_alloc);
  1102. if (BE (new_buffer == NULL, 0))
  1103. return REG_ESPACE;
  1104. dest->elems = new_buffer;
  1105. dest->alloc = new_alloc;
  1106. }
  1107. if (BE (dest->nelem == 0, 0))
  1108. {
  1109. dest->nelem = src->nelem;
  1110. memcpy (dest->elems, src->elems, src->nelem * sizeof (Idx));
  1111. return REG_NOERROR;
  1112. }
  1113. /* Copy into the top of DEST the items of SRC that are not
  1114. found in DEST. Maybe we could binary search in DEST? */
  1115. for (sbase = dest->nelem + 2 * src->nelem,
  1116. is = src->nelem - 1, id = dest->nelem - 1;
  1117. REG_VALID_INDEX (is) && REG_VALID_INDEX (id); )
  1118. {
  1119. if (dest->elems[id] == src->elems[is])
  1120. is--, id--;
  1121. else if (dest->elems[id] < src->elems[is])
  1122. dest->elems[--sbase] = src->elems[is--];
  1123. else /* if (dest->elems[id] > src->elems[is]) */
  1124. --id;
  1125. }
  1126. if (REG_VALID_INDEX (is))
  1127. {
  1128. /* If DEST is exhausted, the remaining items of SRC must be unique. */
  1129. sbase -= is + 1;
  1130. memcpy (dest->elems + sbase, src->elems, (is + 1) * sizeof (Idx));
  1131. }
  1132. id = dest->nelem - 1;
  1133. is = dest->nelem + 2 * src->nelem - 1;
  1134. delta = is - sbase + 1;
  1135. if (delta == 0)
  1136. return REG_NOERROR;
  1137. /* Now copy. When DELTA becomes zero, the remaining
  1138. DEST elements are already in place. */
  1139. dest->nelem += delta;
  1140. for (;;)
  1141. {
  1142. if (dest->elems[is] > dest->elems[id])
  1143. {
  1144. /* Copy from the top. */
  1145. dest->elems[id + delta--] = dest->elems[is--];
  1146. if (delta == 0)
  1147. break;
  1148. }
  1149. else
  1150. {
  1151. /* Slide from the bottom. */
  1152. dest->elems[id + delta] = dest->elems[id];
  1153. if (! REG_VALID_INDEX (--id))
  1154. {
  1155. /* Copy remaining SRC elements. */
  1156. memcpy (dest->elems, dest->elems + sbase,
  1157. delta * sizeof (Idx));
  1158. break;
  1159. }
  1160. }
  1161. }
  1162. return REG_NOERROR;
  1163. }
  1164. /* Insert the new element ELEM to the re_node_set* SET.
  1165. SET should not already have ELEM.
  1166. Return true if successful. */
  1167. static bool
  1168. internal_function __attribute_warn_unused_result__
  1169. re_node_set_insert (re_node_set *set, Idx elem)
  1170. {
  1171. Idx idx;
  1172. /* In case the set is empty. */
  1173. if (set->alloc == 0)
  1174. return BE (re_node_set_init_1 (set, elem) == REG_NOERROR, 1);
  1175. if (BE (set->nelem, 0) == 0)
  1176. {
  1177. /* We already guaranteed above that set->alloc != 0. */
  1178. set->elems[0] = elem;
  1179. ++set->nelem;
  1180. return true;
  1181. }
  1182. /* Realloc if we need. */
  1183. if (set->alloc == set->nelem)
  1184. {
  1185. Idx *new_elems;
  1186. set->alloc = set->alloc * 2;
  1187. new_elems = re_realloc (set->elems, Idx, set->alloc);
  1188. if (BE (new_elems == NULL, 0))
  1189. return false;
  1190. set->elems = new_elems;
  1191. }
  1192. /* Move the elements which follows the new element. Test the
  1193. first element separately to skip a check in the inner loop. */
  1194. if (elem < set->elems[0])
  1195. {
  1196. idx = 0;
  1197. for (idx = set->nelem; idx > 0; idx--)
  1198. set->elems[idx] = set->elems[idx - 1];
  1199. }
  1200. else
  1201. {
  1202. for (idx = set->nelem; set->elems[idx - 1] > elem; idx--)
  1203. set->elems[idx] = set->elems[idx - 1];
  1204. }
  1205. /* Insert the new element. */
  1206. set->elems[idx] = elem;
  1207. ++set->nelem;
  1208. return true;
  1209. }
  1210. /* Insert the new element ELEM to the re_node_set* SET.
  1211. SET should not already have any element greater than or equal to ELEM.
  1212. Return true if successful. */
  1213. static bool
  1214. internal_function __attribute_warn_unused_result__
  1215. re_node_set_insert_last (re_node_set *set, Idx elem)
  1216. {
  1217. /* Realloc if we need. */
  1218. if (set->alloc == set->nelem)
  1219. {
  1220. Idx *new_elems;
  1221. set->alloc = (set->alloc + 1) * 2;
  1222. new_elems = re_realloc (set->elems, Idx, set->alloc);
  1223. if (BE (new_elems == NULL, 0))
  1224. return false;
  1225. set->elems = new_elems;
  1226. }
  1227. /* Insert the new element. */
  1228. set->elems[set->nelem++] = elem;
  1229. return true;
  1230. }
  1231. /* Compare two node sets SET1 and SET2.
  1232. Return true if SET1 and SET2 are equivalent. */
  1233. static bool
  1234. internal_function __attribute__ ((pure))
  1235. re_node_set_compare (const re_node_set *set1, const re_node_set *set2)
  1236. {
  1237. Idx i;
  1238. if (set1 == NULL || set2 == NULL || set1->nelem != set2->nelem)
  1239. return false;
  1240. for (i = set1->nelem ; REG_VALID_INDEX (--i) ; )
  1241. if (set1->elems[i] != set2->elems[i])
  1242. return false;
  1243. return true;
  1244. }
  1245. /* Return (idx + 1) if SET contains the element ELEM, return 0 otherwise. */
  1246. static Idx
  1247. internal_function __attribute__ ((pure))
  1248. re_node_set_contains (const re_node_set *set, Idx elem)
  1249. {
  1250. __re_size_t idx, right, mid;
  1251. if (! REG_VALID_NONZERO_INDEX (set->nelem))
  1252. return 0;
  1253. /* Binary search the element. */
  1254. idx = 0;
  1255. right = set->nelem - 1;
  1256. while (idx < right)
  1257. {
  1258. mid = (idx + right) / 2;
  1259. if (set->elems[mid] < elem)
  1260. idx = mid + 1;
  1261. else
  1262. right = mid;
  1263. }
  1264. return set->elems[idx] == elem ? idx + 1 : 0;
  1265. }
  1266. static void
  1267. internal_function
  1268. re_node_set_remove_at (re_node_set *set, Idx idx)
  1269. {
  1270. if (idx < 0 || idx >= set->nelem)
  1271. return;
  1272. --set->nelem;
  1273. for (; idx < set->nelem; idx++)
  1274. set->elems[idx] = set->elems[idx + 1];
  1275. }
  1276. /* Add the token TOKEN to dfa->nodes, and return the index of the token.
  1277. Or return REG_MISSING if an error occurred. */
  1278. static Idx
  1279. internal_function
  1280. re_dfa_add_node (re_dfa_t *dfa, re_token_t token)
  1281. {
  1282. if (BE (dfa->nodes_len >= dfa->nodes_alloc, 0))
  1283. {
  1284. size_t new_nodes_alloc = dfa->nodes_alloc * 2;
  1285. Idx *new_nexts, *new_indices;
  1286. re_node_set *new_edests, *new_eclosures;
  1287. re_token_t *new_nodes;
  1288. /* Avoid overflows in realloc. */
  1289. const size_t max_object_size = MAX (sizeof (re_token_t),
  1290. MAX (sizeof (re_node_set),
  1291. sizeof (Idx)));
  1292. if (BE (MIN (IDX_MAX, SIZE_MAX / max_object_size) < new_nodes_alloc, 0))
  1293. return REG_MISSING;
  1294. new_nodes = re_realloc (dfa->nodes, re_token_t, new_nodes_alloc);
  1295. if (BE (new_nodes == NULL, 0))
  1296. return REG_MISSING;
  1297. dfa->nodes = new_nodes;
  1298. new_nexts = re_realloc (dfa->nexts, Idx, new_nodes_alloc);
  1299. new_indices = re_realloc (dfa->org_indices, Idx, new_nodes_alloc);
  1300. new_edests = re_realloc (dfa->edests, re_node_set, new_nodes_alloc);
  1301. new_eclosures = re_realloc (dfa->eclosures, re_node_set, new_nodes_alloc);
  1302. if (BE (new_nexts == NULL || new_indices == NULL
  1303. || new_edests == NULL || new_eclosures == NULL, 0))
  1304. return REG_MISSING;
  1305. dfa->nexts = new_nexts;
  1306. dfa->org_indices = new_indices;
  1307. dfa->edests = new_edests;
  1308. dfa->eclosures = new_eclosures;
  1309. dfa->nodes_alloc = new_nodes_alloc;
  1310. }
  1311. dfa->nodes[dfa->nodes_len] = token;
  1312. dfa->nodes[dfa->nodes_len].constraint = 0;
  1313. #ifdef RE_ENABLE_I18N
  1314. dfa->nodes[dfa->nodes_len].accept_mb =
  1315. ((token.type == OP_PERIOD && dfa->mb_cur_max > 1)
  1316. || token.type == COMPLEX_BRACKET);
  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 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 (! re_node_set_insert_last (&newstate->non_eps_nodes, elem))
  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 independent 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. }