regex_internal.c 48 KB

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