regex_internal.c 44 KB

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