regexec.c 125 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252
  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 reg_errcode_t match_ctx_init (re_match_context_t *cache, int eflags,
  17. Idx n);
  18. static void match_ctx_clean (re_match_context_t *mctx);
  19. static void match_ctx_free (re_match_context_t *cache);
  20. static reg_errcode_t match_ctx_add_entry (re_match_context_t *cache, Idx node,
  21. Idx str_idx, Idx from, Idx to);
  22. static Idx search_cur_bkref_entry (const re_match_context_t *mctx, Idx str_idx);
  23. static reg_errcode_t match_ctx_add_subtop (re_match_context_t *mctx, Idx node,
  24. Idx str_idx);
  25. static re_sub_match_last_t * match_ctx_add_sublast (re_sub_match_top_t *subtop,
  26. Idx node, Idx str_idx);
  27. static void sift_ctx_init (re_sift_context_t *sctx, re_dfastate_t **sifted_sts,
  28. re_dfastate_t **limited_sts, Idx last_node,
  29. Idx last_str_idx);
  30. static reg_errcode_t re_search_internal (const regex_t *preg,
  31. const char *string, Idx length,
  32. Idx start, Idx last_start, Idx stop,
  33. size_t nmatch, regmatch_t pmatch[],
  34. int eflags);
  35. static regoff_t re_search_2_stub (struct re_pattern_buffer *bufp,
  36. const char *string1, Idx length1,
  37. const char *string2, Idx length2,
  38. Idx start, regoff_t range,
  39. struct re_registers *regs,
  40. Idx stop, bool ret_len);
  41. static regoff_t re_search_stub (struct re_pattern_buffer *bufp,
  42. const char *string, Idx length, Idx start,
  43. regoff_t range, Idx stop,
  44. struct re_registers *regs,
  45. bool ret_len);
  46. static unsigned re_copy_regs (struct re_registers *regs, regmatch_t *pmatch,
  47. Idx nregs, int regs_allocated);
  48. static reg_errcode_t prune_impossible_nodes (re_match_context_t *mctx);
  49. static Idx check_matching (re_match_context_t *mctx, bool fl_longest_match,
  50. Idx *p_match_first);
  51. static Idx check_halt_state_context (const re_match_context_t *mctx,
  52. const re_dfastate_t *state, Idx idx);
  53. static void update_regs (const re_dfa_t *dfa, regmatch_t *pmatch,
  54. regmatch_t *prev_idx_match, Idx cur_node,
  55. Idx cur_idx, Idx nmatch);
  56. static reg_errcode_t push_fail_stack (struct re_fail_stack_t *fs,
  57. Idx str_idx, Idx dest_node, Idx nregs,
  58. regmatch_t *regs,
  59. re_node_set *eps_via_nodes);
  60. static reg_errcode_t set_regs (const regex_t *preg,
  61. const re_match_context_t *mctx,
  62. size_t nmatch, regmatch_t *pmatch,
  63. bool fl_backtrack);
  64. static reg_errcode_t free_fail_stack_return (struct re_fail_stack_t *fs);
  65. #ifdef RE_ENABLE_I18N
  66. static int sift_states_iter_mb (const re_match_context_t *mctx,
  67. re_sift_context_t *sctx,
  68. Idx node_idx, Idx str_idx, Idx max_str_idx);
  69. #endif /* RE_ENABLE_I18N */
  70. static reg_errcode_t sift_states_backward (const re_match_context_t *mctx,
  71. re_sift_context_t *sctx);
  72. static reg_errcode_t build_sifted_states (const re_match_context_t *mctx,
  73. re_sift_context_t *sctx, Idx str_idx,
  74. re_node_set *cur_dest);
  75. static reg_errcode_t update_cur_sifted_state (const re_match_context_t *mctx,
  76. re_sift_context_t *sctx,
  77. Idx str_idx,
  78. re_node_set *dest_nodes);
  79. static reg_errcode_t add_epsilon_src_nodes (const re_dfa_t *dfa,
  80. re_node_set *dest_nodes,
  81. const re_node_set *candidates);
  82. static bool check_dst_limits (const re_match_context_t *mctx,
  83. const re_node_set *limits,
  84. Idx dst_node, Idx dst_idx, Idx src_node,
  85. Idx src_idx);
  86. static int check_dst_limits_calc_pos_1 (const re_match_context_t *mctx,
  87. int boundaries, Idx subexp_idx,
  88. Idx from_node, Idx bkref_idx);
  89. static int check_dst_limits_calc_pos (const re_match_context_t *mctx,
  90. Idx limit, Idx subexp_idx,
  91. Idx node, Idx str_idx,
  92. Idx bkref_idx);
  93. static reg_errcode_t check_subexp_limits (const re_dfa_t *dfa,
  94. re_node_set *dest_nodes,
  95. const re_node_set *candidates,
  96. re_node_set *limits,
  97. struct re_backref_cache_entry *bkref_ents,
  98. Idx str_idx);
  99. static reg_errcode_t sift_states_bkref (const re_match_context_t *mctx,
  100. re_sift_context_t *sctx,
  101. Idx str_idx, const re_node_set *candidates);
  102. static reg_errcode_t merge_state_array (const re_dfa_t *dfa,
  103. re_dfastate_t **dst,
  104. re_dfastate_t **src, Idx num);
  105. static re_dfastate_t *find_recover_state (reg_errcode_t *err,
  106. re_match_context_t *mctx);
  107. static re_dfastate_t *transit_state (reg_errcode_t *err,
  108. re_match_context_t *mctx,
  109. re_dfastate_t *state);
  110. static re_dfastate_t *merge_state_with_log (reg_errcode_t *err,
  111. re_match_context_t *mctx,
  112. re_dfastate_t *next_state);
  113. static reg_errcode_t check_subexp_matching_top (re_match_context_t *mctx,
  114. re_node_set *cur_nodes,
  115. Idx str_idx);
  116. #if 0
  117. static re_dfastate_t *transit_state_sb (reg_errcode_t *err,
  118. re_match_context_t *mctx,
  119. re_dfastate_t *pstate);
  120. #endif
  121. #ifdef RE_ENABLE_I18N
  122. static reg_errcode_t transit_state_mb (re_match_context_t *mctx,
  123. re_dfastate_t *pstate);
  124. #endif /* RE_ENABLE_I18N */
  125. static reg_errcode_t transit_state_bkref (re_match_context_t *mctx,
  126. const re_node_set *nodes);
  127. static reg_errcode_t get_subexp (re_match_context_t *mctx,
  128. Idx bkref_node, Idx bkref_str_idx);
  129. static reg_errcode_t get_subexp_sub (re_match_context_t *mctx,
  130. const re_sub_match_top_t *sub_top,
  131. re_sub_match_last_t *sub_last,
  132. Idx bkref_node, Idx bkref_str);
  133. static Idx find_subexp_node (const re_dfa_t *dfa, const re_node_set *nodes,
  134. Idx subexp_idx, int type);
  135. static reg_errcode_t check_arrival (re_match_context_t *mctx,
  136. state_array_t *path, Idx top_node,
  137. Idx top_str, Idx last_node, Idx last_str,
  138. int type);
  139. static reg_errcode_t check_arrival_add_next_nodes (re_match_context_t *mctx,
  140. Idx str_idx,
  141. re_node_set *cur_nodes,
  142. re_node_set *next_nodes);
  143. static reg_errcode_t check_arrival_expand_ecl (const re_dfa_t *dfa,
  144. re_node_set *cur_nodes,
  145. Idx ex_subexp, int type);
  146. static reg_errcode_t check_arrival_expand_ecl_sub (const re_dfa_t *dfa,
  147. re_node_set *dst_nodes,
  148. Idx target, Idx ex_subexp,
  149. int type);
  150. static reg_errcode_t expand_bkref_cache (re_match_context_t *mctx,
  151. re_node_set *cur_nodes, Idx cur_str,
  152. Idx subexp_num, int type);
  153. static bool build_trtable (const re_dfa_t *dfa, re_dfastate_t *state);
  154. #ifdef RE_ENABLE_I18N
  155. static int check_node_accept_bytes (const re_dfa_t *dfa, Idx node_idx,
  156. const re_string_t *input, Idx idx);
  157. # ifdef _LIBC
  158. static unsigned int find_collation_sequence_value (const unsigned char *mbs,
  159. size_t name_len);
  160. # endif /* _LIBC */
  161. #endif /* RE_ENABLE_I18N */
  162. static Idx group_nodes_into_DFAstates (const re_dfa_t *dfa,
  163. const re_dfastate_t *state,
  164. re_node_set *states_node,
  165. bitset_t *states_ch);
  166. static bool check_node_accept (const re_match_context_t *mctx,
  167. const re_token_t *node, Idx idx);
  168. static reg_errcode_t extend_buffers (re_match_context_t *mctx, int min_len);
  169. /* Entry point for POSIX code. */
  170. /* regexec searches for a given pattern, specified by PREG, in the
  171. string STRING.
  172. If NMATCH is zero or REG_NOSUB was set in the cflags argument to
  173. 'regcomp', we ignore PMATCH. Otherwise, we assume PMATCH has at
  174. least NMATCH elements, and we set them to the offsets of the
  175. corresponding matched substrings.
  176. EFLAGS specifies "execution flags" which affect matching: if
  177. REG_NOTBOL is set, then ^ does not match at the beginning of the
  178. string; if REG_NOTEOL is set, then $ does not match at the end.
  179. We return 0 if we find a match and REG_NOMATCH if not. */
  180. int
  181. regexec (const regex_t *__restrict preg, const char *__restrict string,
  182. size_t nmatch, regmatch_t pmatch[], int eflags)
  183. {
  184. reg_errcode_t err;
  185. Idx start, length;
  186. re_dfa_t *dfa = preg->buffer;
  187. if (eflags & ~(REG_NOTBOL | REG_NOTEOL | REG_STARTEND))
  188. return REG_BADPAT;
  189. if (eflags & REG_STARTEND)
  190. {
  191. start = pmatch[0].rm_so;
  192. length = pmatch[0].rm_eo;
  193. }
  194. else
  195. {
  196. start = 0;
  197. length = strlen (string);
  198. }
  199. lock_lock (dfa->lock);
  200. if (preg->no_sub)
  201. err = re_search_internal (preg, string, length, start, length,
  202. length, 0, NULL, eflags);
  203. else
  204. err = re_search_internal (preg, string, length, start, length,
  205. length, nmatch, pmatch, eflags);
  206. lock_unlock (dfa->lock);
  207. return err != REG_NOERROR;
  208. }
  209. #ifdef _LIBC
  210. libc_hidden_def (__regexec)
  211. # include <shlib-compat.h>
  212. versioned_symbol (libc, __regexec, regexec, GLIBC_2_3_4);
  213. # if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_3_4)
  214. __typeof__ (__regexec) __compat_regexec;
  215. int
  216. attribute_compat_text_section
  217. __compat_regexec (const regex_t *__restrict preg,
  218. const char *__restrict string, size_t nmatch,
  219. regmatch_t pmatch[], int eflags)
  220. {
  221. return regexec (preg, string, nmatch, pmatch,
  222. eflags & (REG_NOTBOL | REG_NOTEOL));
  223. }
  224. compat_symbol (libc, __compat_regexec, regexec, GLIBC_2_0);
  225. # endif
  226. #endif
  227. /* Entry points for GNU code. */
  228. /* re_match, re_search, re_match_2, re_search_2
  229. The former two functions operate on STRING with length LENGTH,
  230. while the later two operate on concatenation of STRING1 and STRING2
  231. with lengths LENGTH1 and LENGTH2, respectively.
  232. re_match() matches the compiled pattern in BUFP against the string,
  233. starting at index START.
  234. re_search() first tries matching at index START, then it tries to match
  235. starting from index START + 1, and so on. The last start position tried
  236. is START + RANGE. (Thus RANGE = 0 forces re_search to operate the same
  237. way as re_match().)
  238. The parameter STOP of re_{match,search}_2 specifies that no match exceeding
  239. the first STOP characters of the concatenation of the strings should be
  240. concerned.
  241. If REGS is not NULL, and BUFP->no_sub is not set, the offsets of the match
  242. and all groups is stored in REGS. (For the "_2" variants, the offsets are
  243. computed relative to the concatenation, not relative to the individual
  244. strings.)
  245. On success, re_match* functions return the length of the match, re_search*
  246. return the position of the start of the match. Return value -1 means no
  247. match was found and -2 indicates an internal error. */
  248. regoff_t
  249. re_match (struct re_pattern_buffer *bufp, const char *string, Idx length,
  250. Idx start, struct re_registers *regs)
  251. {
  252. return re_search_stub (bufp, string, length, start, 0, length, regs, true);
  253. }
  254. #ifdef _LIBC
  255. weak_alias (__re_match, re_match)
  256. #endif
  257. regoff_t
  258. re_search (struct re_pattern_buffer *bufp, const char *string, Idx length,
  259. Idx start, regoff_t range, struct re_registers *regs)
  260. {
  261. return re_search_stub (bufp, string, length, start, range, length, regs,
  262. false);
  263. }
  264. #ifdef _LIBC
  265. weak_alias (__re_search, re_search)
  266. #endif
  267. regoff_t
  268. re_match_2 (struct re_pattern_buffer *bufp, const char *string1, Idx length1,
  269. const char *string2, Idx length2, Idx start,
  270. struct re_registers *regs, Idx stop)
  271. {
  272. return re_search_2_stub (bufp, string1, length1, string2, length2,
  273. start, 0, regs, stop, true);
  274. }
  275. #ifdef _LIBC
  276. weak_alias (__re_match_2, re_match_2)
  277. #endif
  278. regoff_t
  279. re_search_2 (struct re_pattern_buffer *bufp, const char *string1, Idx length1,
  280. const char *string2, Idx length2, Idx start, regoff_t range,
  281. struct re_registers *regs, Idx stop)
  282. {
  283. return re_search_2_stub (bufp, string1, length1, string2, length2,
  284. start, range, regs, stop, false);
  285. }
  286. #ifdef _LIBC
  287. weak_alias (__re_search_2, re_search_2)
  288. #endif
  289. static regoff_t
  290. re_search_2_stub (struct re_pattern_buffer *bufp, const char *string1,
  291. Idx length1, const char *string2, Idx length2, Idx start,
  292. regoff_t range, struct re_registers *regs,
  293. Idx stop, bool ret_len)
  294. {
  295. const char *str;
  296. regoff_t rval;
  297. Idx len;
  298. char *s = NULL;
  299. if (__glibc_unlikely ((length1 < 0 || length2 < 0 || stop < 0
  300. || INT_ADD_WRAPV (length1, length2, &len))))
  301. return -2;
  302. /* Concatenate the strings. */
  303. if (length2 > 0)
  304. if (length1 > 0)
  305. {
  306. s = re_malloc (char, len);
  307. if (__glibc_unlikely (s == NULL))
  308. return -2;
  309. #ifdef _LIBC
  310. memcpy (__mempcpy (s, string1, length1), string2, length2);
  311. #else
  312. memcpy (s, string1, length1);
  313. memcpy (s + length1, string2, length2);
  314. #endif
  315. str = s;
  316. }
  317. else
  318. str = string2;
  319. else
  320. str = string1;
  321. rval = re_search_stub (bufp, str, len, start, range, stop, regs,
  322. ret_len);
  323. re_free (s);
  324. return rval;
  325. }
  326. /* The parameters have the same meaning as those of re_search.
  327. Additional parameters:
  328. If RET_LEN is true the length of the match is returned (re_match style);
  329. otherwise the position of the match is returned. */
  330. static regoff_t
  331. re_search_stub (struct re_pattern_buffer *bufp, const char *string, Idx length,
  332. Idx start, regoff_t range, Idx stop, struct re_registers *regs,
  333. bool ret_len)
  334. {
  335. reg_errcode_t result;
  336. regmatch_t *pmatch;
  337. Idx nregs;
  338. regoff_t rval;
  339. int eflags = 0;
  340. re_dfa_t *dfa = bufp->buffer;
  341. Idx last_start = start + range;
  342. /* Check for out-of-range. */
  343. if (__glibc_unlikely (start < 0 || start > length))
  344. return -1;
  345. if (__glibc_unlikely (length < last_start
  346. || (0 <= range && last_start < start)))
  347. last_start = length;
  348. else if (__glibc_unlikely (last_start < 0
  349. || (range < 0 && start <= last_start)))
  350. last_start = 0;
  351. lock_lock (dfa->lock);
  352. eflags |= (bufp->not_bol) ? REG_NOTBOL : 0;
  353. eflags |= (bufp->not_eol) ? REG_NOTEOL : 0;
  354. /* Compile fastmap if we haven't yet. */
  355. if (start < last_start && bufp->fastmap != NULL && !bufp->fastmap_accurate)
  356. re_compile_fastmap (bufp);
  357. if (__glibc_unlikely (bufp->no_sub))
  358. regs = NULL;
  359. /* We need at least 1 register. */
  360. if (regs == NULL)
  361. nregs = 1;
  362. else if (__glibc_unlikely (bufp->regs_allocated == REGS_FIXED
  363. && regs->num_regs <= bufp->re_nsub))
  364. {
  365. nregs = regs->num_regs;
  366. if (__glibc_unlikely (nregs < 1))
  367. {
  368. /* Nothing can be copied to regs. */
  369. regs = NULL;
  370. nregs = 1;
  371. }
  372. }
  373. else
  374. nregs = bufp->re_nsub + 1;
  375. pmatch = re_malloc (regmatch_t, nregs);
  376. if (__glibc_unlikely (pmatch == NULL))
  377. {
  378. rval = -2;
  379. goto out;
  380. }
  381. result = re_search_internal (bufp, string, length, start, last_start, stop,
  382. nregs, pmatch, eflags);
  383. rval = 0;
  384. /* I hope we needn't fill their regs with -1's when no match was found. */
  385. if (result != REG_NOERROR)
  386. rval = result == REG_NOMATCH ? -1 : -2;
  387. else if (regs != NULL)
  388. {
  389. /* If caller wants register contents data back, copy them. */
  390. bufp->regs_allocated = re_copy_regs (regs, pmatch, nregs,
  391. bufp->regs_allocated);
  392. if (__glibc_unlikely (bufp->regs_allocated == REGS_UNALLOCATED))
  393. rval = -2;
  394. }
  395. if (__glibc_likely (rval == 0))
  396. {
  397. if (ret_len)
  398. {
  399. DEBUG_ASSERT (pmatch[0].rm_so == start);
  400. rval = pmatch[0].rm_eo - start;
  401. }
  402. else
  403. rval = pmatch[0].rm_so;
  404. }
  405. re_free (pmatch);
  406. out:
  407. lock_unlock (dfa->lock);
  408. return rval;
  409. }
  410. static unsigned
  411. re_copy_regs (struct re_registers *regs, regmatch_t *pmatch, Idx nregs,
  412. int regs_allocated)
  413. {
  414. int rval = REGS_REALLOCATE;
  415. Idx i;
  416. Idx need_regs = nregs + 1;
  417. /* We need one extra element beyond 'num_regs' for the '-1' marker GNU code
  418. uses. */
  419. /* Have the register data arrays been allocated? */
  420. if (regs_allocated == REGS_UNALLOCATED)
  421. { /* No. So allocate them with malloc. */
  422. regs->start = re_malloc (regoff_t, need_regs);
  423. if (__glibc_unlikely (regs->start == NULL))
  424. return REGS_UNALLOCATED;
  425. regs->end = re_malloc (regoff_t, need_regs);
  426. if (__glibc_unlikely (regs->end == NULL))
  427. {
  428. re_free (regs->start);
  429. return REGS_UNALLOCATED;
  430. }
  431. regs->num_regs = need_regs;
  432. }
  433. else if (regs_allocated == REGS_REALLOCATE)
  434. { /* Yes. If we need more elements than were already
  435. allocated, reallocate them. If we need fewer, just
  436. leave it alone. */
  437. if (__glibc_unlikely (need_regs > regs->num_regs))
  438. {
  439. regoff_t *new_start = re_realloc (regs->start, regoff_t, need_regs);
  440. regoff_t *new_end;
  441. if (__glibc_unlikely (new_start == NULL))
  442. return REGS_UNALLOCATED;
  443. new_end = re_realloc (regs->end, regoff_t, need_regs);
  444. if (__glibc_unlikely (new_end == NULL))
  445. {
  446. re_free (new_start);
  447. return REGS_UNALLOCATED;
  448. }
  449. regs->start = new_start;
  450. regs->end = new_end;
  451. regs->num_regs = need_regs;
  452. }
  453. }
  454. else
  455. {
  456. DEBUG_ASSERT (regs_allocated == REGS_FIXED);
  457. /* This function may not be called with REGS_FIXED and nregs too big. */
  458. DEBUG_ASSERT (nregs <= regs->num_regs);
  459. rval = REGS_FIXED;
  460. }
  461. /* Copy the regs. */
  462. for (i = 0; i < nregs; ++i)
  463. {
  464. regs->start[i] = pmatch[i].rm_so;
  465. regs->end[i] = pmatch[i].rm_eo;
  466. }
  467. for ( ; i < regs->num_regs; ++i)
  468. regs->start[i] = regs->end[i] = -1;
  469. return rval;
  470. }
  471. /* Set REGS to hold NUM_REGS registers, storing them in STARTS and
  472. ENDS. Subsequent matches using PATTERN_BUFFER and REGS will use
  473. this memory for recording register information. STARTS and ENDS
  474. must be allocated using the malloc library routine, and must each
  475. be at least NUM_REGS * sizeof (regoff_t) bytes long.
  476. If NUM_REGS == 0, then subsequent matches should allocate their own
  477. register data.
  478. Unless this function is called, the first search or match using
  479. PATTERN_BUFFER will allocate its own register data, without
  480. freeing the old data. */
  481. void
  482. re_set_registers (struct re_pattern_buffer *bufp, struct re_registers *regs,
  483. __re_size_t num_regs, regoff_t *starts, regoff_t *ends)
  484. {
  485. if (num_regs)
  486. {
  487. bufp->regs_allocated = REGS_REALLOCATE;
  488. regs->num_regs = num_regs;
  489. regs->start = starts;
  490. regs->end = ends;
  491. }
  492. else
  493. {
  494. bufp->regs_allocated = REGS_UNALLOCATED;
  495. regs->num_regs = 0;
  496. regs->start = regs->end = NULL;
  497. }
  498. }
  499. #ifdef _LIBC
  500. weak_alias (__re_set_registers, re_set_registers)
  501. #endif
  502. /* Entry points compatible with 4.2 BSD regex library. We don't define
  503. them unless specifically requested. */
  504. #if defined _REGEX_RE_COMP || defined _LIBC
  505. int
  506. # ifdef _LIBC
  507. weak_function
  508. # endif
  509. re_exec (const char *s)
  510. {
  511. return 0 == regexec (&re_comp_buf, s, 0, NULL, 0);
  512. }
  513. #endif /* _REGEX_RE_COMP */
  514. /* Internal entry point. */
  515. /* Searches for a compiled pattern PREG in the string STRING, whose
  516. length is LENGTH. NMATCH, PMATCH, and EFLAGS have the same
  517. meaning as with regexec. LAST_START is START + RANGE, where
  518. START and RANGE have the same meaning as with re_search.
  519. Return REG_NOERROR if we find a match, and REG_NOMATCH if not,
  520. otherwise return the error code.
  521. Note: We assume front end functions already check ranges.
  522. (0 <= LAST_START && LAST_START <= LENGTH) */
  523. static reg_errcode_t
  524. __attribute_warn_unused_result__
  525. re_search_internal (const regex_t *preg, const char *string, Idx length,
  526. Idx start, Idx last_start, Idx stop, size_t nmatch,
  527. regmatch_t pmatch[], int eflags)
  528. {
  529. reg_errcode_t err;
  530. const re_dfa_t *dfa = preg->buffer;
  531. Idx left_lim, right_lim;
  532. int incr;
  533. bool fl_longest_match;
  534. int match_kind;
  535. Idx match_first;
  536. Idx match_last = -1;
  537. Idx extra_nmatch;
  538. bool sb;
  539. int ch;
  540. re_match_context_t mctx = { .dfa = dfa };
  541. char *fastmap = ((preg->fastmap != NULL && preg->fastmap_accurate
  542. && start != last_start && !preg->can_be_null)
  543. ? preg->fastmap : NULL);
  544. RE_TRANSLATE_TYPE t = preg->translate;
  545. extra_nmatch = (nmatch > preg->re_nsub) ? nmatch - (preg->re_nsub + 1) : 0;
  546. nmatch -= extra_nmatch;
  547. /* Check if the DFA haven't been compiled. */
  548. if (__glibc_unlikely (preg->used == 0 || dfa->init_state == NULL
  549. || dfa->init_state_word == NULL
  550. || dfa->init_state_nl == NULL
  551. || dfa->init_state_begbuf == NULL))
  552. return REG_NOMATCH;
  553. /* We assume front-end functions already check them. */
  554. DEBUG_ASSERT (0 <= last_start && last_start <= length);
  555. /* If initial states with non-begbuf contexts have no elements,
  556. the regex must be anchored. If preg->newline_anchor is set,
  557. we'll never use init_state_nl, so do not check it. */
  558. if (dfa->init_state->nodes.nelem == 0
  559. && dfa->init_state_word->nodes.nelem == 0
  560. && (dfa->init_state_nl->nodes.nelem == 0
  561. || !preg->newline_anchor))
  562. {
  563. if (start != 0 && last_start != 0)
  564. return REG_NOMATCH;
  565. start = last_start = 0;
  566. }
  567. /* We must check the longest matching, if nmatch > 0. */
  568. fl_longest_match = (nmatch != 0 || dfa->nbackref);
  569. err = re_string_allocate (&mctx.input, string, length, dfa->nodes_len + 1,
  570. preg->translate, (preg->syntax & RE_ICASE) != 0,
  571. dfa);
  572. if (__glibc_unlikely (err != REG_NOERROR))
  573. goto free_return;
  574. mctx.input.stop = stop;
  575. mctx.input.raw_stop = stop;
  576. mctx.input.newline_anchor = preg->newline_anchor;
  577. err = match_ctx_init (&mctx, eflags, dfa->nbackref * 2);
  578. if (__glibc_unlikely (err != REG_NOERROR))
  579. goto free_return;
  580. /* We will log all the DFA states through which the dfa pass,
  581. if nmatch > 1, or this dfa has "multibyte node", which is a
  582. back-reference or a node which can accept multibyte character or
  583. multi character collating element. */
  584. if (nmatch > 1 || dfa->has_mb_node)
  585. {
  586. /* Avoid overflow. */
  587. if (__glibc_unlikely ((MIN (IDX_MAX, SIZE_MAX / sizeof (re_dfastate_t *))
  588. <= mctx.input.bufs_len)))
  589. {
  590. err = REG_ESPACE;
  591. goto free_return;
  592. }
  593. mctx.state_log = re_malloc (re_dfastate_t *, mctx.input.bufs_len + 1);
  594. if (__glibc_unlikely (mctx.state_log == NULL))
  595. {
  596. err = REG_ESPACE;
  597. goto free_return;
  598. }
  599. }
  600. match_first = start;
  601. mctx.input.tip_context = (eflags & REG_NOTBOL) ? CONTEXT_BEGBUF
  602. : CONTEXT_NEWLINE | CONTEXT_BEGBUF;
  603. /* Check incrementally whether the input string matches. */
  604. incr = (last_start < start) ? -1 : 1;
  605. left_lim = (last_start < start) ? last_start : start;
  606. right_lim = (last_start < start) ? start : last_start;
  607. sb = dfa->mb_cur_max == 1;
  608. match_kind =
  609. (fastmap
  610. ? ((sb || !(preg->syntax & RE_ICASE || t) ? 4 : 0)
  611. | (start <= last_start ? 2 : 0)
  612. | (t != NULL ? 1 : 0))
  613. : 8);
  614. for (;; match_first += incr)
  615. {
  616. err = REG_NOMATCH;
  617. if (match_first < left_lim || right_lim < match_first)
  618. goto free_return;
  619. /* Advance as rapidly as possible through the string, until we
  620. find a plausible place to start matching. This may be done
  621. with varying efficiency, so there are various possibilities:
  622. only the most common of them are specialized, in order to
  623. save on code size. We use a switch statement for speed. */
  624. switch (match_kind)
  625. {
  626. case 8:
  627. /* No fastmap. */
  628. break;
  629. case 7:
  630. /* Fastmap with single-byte translation, match forward. */
  631. while (__glibc_likely (match_first < right_lim)
  632. && !fastmap[t[(unsigned char) string[match_first]]])
  633. ++match_first;
  634. goto forward_match_found_start_or_reached_end;
  635. case 6:
  636. /* Fastmap without translation, match forward. */
  637. while (__glibc_likely (match_first < right_lim)
  638. && !fastmap[(unsigned char) string[match_first]])
  639. ++match_first;
  640. forward_match_found_start_or_reached_end:
  641. if (__glibc_unlikely (match_first == right_lim))
  642. {
  643. ch = match_first >= length
  644. ? 0 : (unsigned char) string[match_first];
  645. if (!fastmap[t ? t[ch] : ch])
  646. goto free_return;
  647. }
  648. break;
  649. case 4:
  650. case 5:
  651. /* Fastmap without multi-byte translation, match backwards. */
  652. while (match_first >= left_lim)
  653. {
  654. ch = match_first >= length
  655. ? 0 : (unsigned char) string[match_first];
  656. if (fastmap[t ? t[ch] : ch])
  657. break;
  658. --match_first;
  659. }
  660. if (match_first < left_lim)
  661. goto free_return;
  662. break;
  663. default:
  664. /* In this case, we can't determine easily the current byte,
  665. since it might be a component byte of a multibyte
  666. character. Then we use the constructed buffer instead. */
  667. for (;;)
  668. {
  669. /* If MATCH_FIRST is out of the valid range, reconstruct the
  670. buffers. */
  671. __re_size_t offset = match_first - mctx.input.raw_mbs_idx;
  672. if (__glibc_unlikely (offset
  673. >= (__re_size_t) mctx.input.valid_raw_len))
  674. {
  675. err = re_string_reconstruct (&mctx.input, match_first,
  676. eflags);
  677. if (__glibc_unlikely (err != REG_NOERROR))
  678. goto free_return;
  679. offset = match_first - mctx.input.raw_mbs_idx;
  680. }
  681. /* If MATCH_FIRST is out of the buffer, leave it as '\0'.
  682. Note that MATCH_FIRST must not be smaller than 0. */
  683. ch = (match_first >= length
  684. ? 0 : re_string_byte_at (&mctx.input, offset));
  685. if (fastmap[ch])
  686. break;
  687. match_first += incr;
  688. if (match_first < left_lim || match_first > right_lim)
  689. {
  690. err = REG_NOMATCH;
  691. goto free_return;
  692. }
  693. }
  694. break;
  695. }
  696. /* Reconstruct the buffers so that the matcher can assume that
  697. the matching starts from the beginning of the buffer. */
  698. err = re_string_reconstruct (&mctx.input, match_first, eflags);
  699. if (__glibc_unlikely (err != REG_NOERROR))
  700. goto free_return;
  701. #ifdef RE_ENABLE_I18N
  702. /* Don't consider this char as a possible match start if it part,
  703. yet isn't the head, of a multibyte character. */
  704. if (!sb && !re_string_first_byte (&mctx.input, 0))
  705. continue;
  706. #endif
  707. /* It seems to be appropriate one, then use the matcher. */
  708. /* We assume that the matching starts from 0. */
  709. mctx.state_log_top = mctx.nbkref_ents = mctx.max_mb_elem_len = 0;
  710. match_last = check_matching (&mctx, fl_longest_match,
  711. start <= last_start ? &match_first : NULL);
  712. if (match_last != -1)
  713. {
  714. if (__glibc_unlikely (match_last == -2))
  715. {
  716. err = REG_ESPACE;
  717. goto free_return;
  718. }
  719. else
  720. {
  721. mctx.match_last = match_last;
  722. if ((!preg->no_sub && nmatch > 1) || dfa->nbackref)
  723. {
  724. re_dfastate_t *pstate = mctx.state_log[match_last];
  725. mctx.last_node = check_halt_state_context (&mctx, pstate,
  726. match_last);
  727. }
  728. if ((!preg->no_sub && nmatch > 1 && dfa->has_plural_match)
  729. || dfa->nbackref)
  730. {
  731. err = prune_impossible_nodes (&mctx);
  732. if (err == REG_NOERROR)
  733. break;
  734. if (__glibc_unlikely (err != REG_NOMATCH))
  735. goto free_return;
  736. match_last = -1;
  737. }
  738. else
  739. break; /* We found a match. */
  740. }
  741. }
  742. match_ctx_clean (&mctx);
  743. }
  744. DEBUG_ASSERT (match_last != -1);
  745. DEBUG_ASSERT (err == REG_NOERROR);
  746. /* Set pmatch[] if we need. */
  747. if (nmatch > 0)
  748. {
  749. Idx reg_idx;
  750. /* Initialize registers. */
  751. for (reg_idx = 1; reg_idx < nmatch; ++reg_idx)
  752. pmatch[reg_idx].rm_so = pmatch[reg_idx].rm_eo = -1;
  753. /* Set the points where matching start/end. */
  754. pmatch[0].rm_so = 0;
  755. pmatch[0].rm_eo = mctx.match_last;
  756. /* FIXME: This function should fail if mctx.match_last exceeds
  757. the maximum possible regoff_t value. We need a new error
  758. code REG_OVERFLOW. */
  759. if (!preg->no_sub && nmatch > 1)
  760. {
  761. err = set_regs (preg, &mctx, nmatch, pmatch,
  762. dfa->has_plural_match && dfa->nbackref > 0);
  763. if (__glibc_unlikely (err != REG_NOERROR))
  764. goto free_return;
  765. }
  766. /* At last, add the offset to each register, since we slid
  767. the buffers so that we could assume that the matching starts
  768. from 0. */
  769. for (reg_idx = 0; reg_idx < nmatch; ++reg_idx)
  770. if (pmatch[reg_idx].rm_so != -1)
  771. {
  772. #ifdef RE_ENABLE_I18N
  773. if (__glibc_unlikely (mctx.input.offsets_needed != 0))
  774. {
  775. pmatch[reg_idx].rm_so =
  776. (pmatch[reg_idx].rm_so == mctx.input.valid_len
  777. ? mctx.input.valid_raw_len
  778. : mctx.input.offsets[pmatch[reg_idx].rm_so]);
  779. pmatch[reg_idx].rm_eo =
  780. (pmatch[reg_idx].rm_eo == mctx.input.valid_len
  781. ? mctx.input.valid_raw_len
  782. : mctx.input.offsets[pmatch[reg_idx].rm_eo]);
  783. }
  784. #else
  785. DEBUG_ASSERT (mctx.input.offsets_needed == 0);
  786. #endif
  787. pmatch[reg_idx].rm_so += match_first;
  788. pmatch[reg_idx].rm_eo += match_first;
  789. }
  790. for (reg_idx = 0; reg_idx < extra_nmatch; ++reg_idx)
  791. {
  792. pmatch[nmatch + reg_idx].rm_so = -1;
  793. pmatch[nmatch + reg_idx].rm_eo = -1;
  794. }
  795. if (dfa->subexp_map)
  796. for (reg_idx = 0; reg_idx + 1 < nmatch; reg_idx++)
  797. if (dfa->subexp_map[reg_idx] != reg_idx)
  798. {
  799. pmatch[reg_idx + 1].rm_so
  800. = pmatch[dfa->subexp_map[reg_idx] + 1].rm_so;
  801. pmatch[reg_idx + 1].rm_eo
  802. = pmatch[dfa->subexp_map[reg_idx] + 1].rm_eo;
  803. }
  804. }
  805. free_return:
  806. re_free (mctx.state_log);
  807. if (dfa->nbackref)
  808. match_ctx_free (&mctx);
  809. re_string_destruct (&mctx.input);
  810. return err;
  811. }
  812. static reg_errcode_t
  813. __attribute_warn_unused_result__
  814. prune_impossible_nodes (re_match_context_t *mctx)
  815. {
  816. const re_dfa_t *const dfa = mctx->dfa;
  817. Idx halt_node, match_last;
  818. reg_errcode_t ret;
  819. re_dfastate_t **sifted_states;
  820. re_dfastate_t **lim_states = NULL;
  821. re_sift_context_t sctx;
  822. DEBUG_ASSERT (mctx->state_log != NULL);
  823. match_last = mctx->match_last;
  824. halt_node = mctx->last_node;
  825. /* Avoid overflow. */
  826. if (__glibc_unlikely (MIN (IDX_MAX, SIZE_MAX / sizeof (re_dfastate_t *))
  827. <= match_last))
  828. return REG_ESPACE;
  829. sifted_states = re_malloc (re_dfastate_t *, match_last + 1);
  830. if (__glibc_unlikely (sifted_states == NULL))
  831. {
  832. ret = REG_ESPACE;
  833. goto free_return;
  834. }
  835. if (dfa->nbackref)
  836. {
  837. lim_states = re_malloc (re_dfastate_t *, match_last + 1);
  838. if (__glibc_unlikely (lim_states == NULL))
  839. {
  840. ret = REG_ESPACE;
  841. goto free_return;
  842. }
  843. while (1)
  844. {
  845. memset (lim_states, '\0',
  846. sizeof (re_dfastate_t *) * (match_last + 1));
  847. sift_ctx_init (&sctx, sifted_states, lim_states, halt_node,
  848. match_last);
  849. ret = sift_states_backward (mctx, &sctx);
  850. re_node_set_free (&sctx.limits);
  851. if (__glibc_unlikely (ret != REG_NOERROR))
  852. goto free_return;
  853. if (sifted_states[0] != NULL || lim_states[0] != NULL)
  854. break;
  855. do
  856. {
  857. --match_last;
  858. if (match_last < 0)
  859. {
  860. ret = REG_NOMATCH;
  861. goto free_return;
  862. }
  863. } while (mctx->state_log[match_last] == NULL
  864. || !mctx->state_log[match_last]->halt);
  865. halt_node = check_halt_state_context (mctx,
  866. mctx->state_log[match_last],
  867. match_last);
  868. }
  869. ret = merge_state_array (dfa, sifted_states, lim_states,
  870. match_last + 1);
  871. re_free (lim_states);
  872. lim_states = NULL;
  873. if (__glibc_unlikely (ret != REG_NOERROR))
  874. goto free_return;
  875. }
  876. else
  877. {
  878. sift_ctx_init (&sctx, sifted_states, lim_states, halt_node, match_last);
  879. ret = sift_states_backward (mctx, &sctx);
  880. re_node_set_free (&sctx.limits);
  881. if (__glibc_unlikely (ret != REG_NOERROR))
  882. goto free_return;
  883. if (sifted_states[0] == NULL)
  884. {
  885. ret = REG_NOMATCH;
  886. goto free_return;
  887. }
  888. }
  889. re_free (mctx->state_log);
  890. mctx->state_log = sifted_states;
  891. sifted_states = NULL;
  892. mctx->last_node = halt_node;
  893. mctx->match_last = match_last;
  894. ret = REG_NOERROR;
  895. free_return:
  896. re_free (sifted_states);
  897. re_free (lim_states);
  898. return ret;
  899. }
  900. /* Acquire an initial state and return it.
  901. We must select appropriate initial state depending on the context,
  902. since initial states may have constraints like "\<", "^", etc.. */
  903. static inline re_dfastate_t *
  904. __attribute__ ((always_inline))
  905. acquire_init_state_context (reg_errcode_t *err, const re_match_context_t *mctx,
  906. Idx idx)
  907. {
  908. const re_dfa_t *const dfa = mctx->dfa;
  909. if (dfa->init_state->has_constraint)
  910. {
  911. unsigned int context;
  912. context = re_string_context_at (&mctx->input, idx - 1, mctx->eflags);
  913. if (IS_WORD_CONTEXT (context))
  914. return dfa->init_state_word;
  915. else if (IS_ORDINARY_CONTEXT (context))
  916. return dfa->init_state;
  917. else if (IS_BEGBUF_CONTEXT (context) && IS_NEWLINE_CONTEXT (context))
  918. return dfa->init_state_begbuf;
  919. else if (IS_NEWLINE_CONTEXT (context))
  920. return dfa->init_state_nl;
  921. else if (IS_BEGBUF_CONTEXT (context))
  922. {
  923. /* It is relatively rare case, then calculate on demand. */
  924. return re_acquire_state_context (err, dfa,
  925. dfa->init_state->entrance_nodes,
  926. context);
  927. }
  928. else
  929. /* Must not happen? */
  930. return dfa->init_state;
  931. }
  932. else
  933. return dfa->init_state;
  934. }
  935. /* Check whether the regular expression match input string INPUT or not,
  936. and return the index where the matching end. Return -1 if
  937. there is no match, and return -2 in case of an error.
  938. FL_LONGEST_MATCH means we want the POSIX longest matching.
  939. If P_MATCH_FIRST is not NULL, and the match fails, it is set to the
  940. next place where we may want to try matching.
  941. Note that the matcher assumes that the matching starts from the current
  942. index of the buffer. */
  943. static Idx
  944. __attribute_warn_unused_result__
  945. check_matching (re_match_context_t *mctx, bool fl_longest_match,
  946. Idx *p_match_first)
  947. {
  948. const re_dfa_t *const dfa = mctx->dfa;
  949. reg_errcode_t err;
  950. Idx match = 0;
  951. Idx match_last = -1;
  952. Idx cur_str_idx = re_string_cur_idx (&mctx->input);
  953. re_dfastate_t *cur_state;
  954. bool at_init_state = p_match_first != NULL;
  955. Idx next_start_idx = cur_str_idx;
  956. err = REG_NOERROR;
  957. cur_state = acquire_init_state_context (&err, mctx, cur_str_idx);
  958. /* An initial state must not be NULL (invalid). */
  959. if (__glibc_unlikely (cur_state == NULL))
  960. {
  961. DEBUG_ASSERT (err == REG_ESPACE);
  962. return -2;
  963. }
  964. if (mctx->state_log != NULL)
  965. {
  966. mctx->state_log[cur_str_idx] = cur_state;
  967. /* Check OP_OPEN_SUBEXP in the initial state in case that we use them
  968. later. E.g. Processing back references. */
  969. if (__glibc_unlikely (dfa->nbackref))
  970. {
  971. at_init_state = false;
  972. err = check_subexp_matching_top (mctx, &cur_state->nodes, 0);
  973. if (__glibc_unlikely (err != REG_NOERROR))
  974. return err;
  975. if (cur_state->has_backref)
  976. {
  977. err = transit_state_bkref (mctx, &cur_state->nodes);
  978. if (__glibc_unlikely (err != REG_NOERROR))
  979. return err;
  980. }
  981. }
  982. }
  983. /* If the RE accepts NULL string. */
  984. if (__glibc_unlikely (cur_state->halt))
  985. {
  986. if (!cur_state->has_constraint
  987. || check_halt_state_context (mctx, cur_state, cur_str_idx))
  988. {
  989. if (!fl_longest_match)
  990. return cur_str_idx;
  991. else
  992. {
  993. match_last = cur_str_idx;
  994. match = 1;
  995. }
  996. }
  997. }
  998. while (!re_string_eoi (&mctx->input))
  999. {
  1000. re_dfastate_t *old_state = cur_state;
  1001. Idx next_char_idx = re_string_cur_idx (&mctx->input) + 1;
  1002. if ((__glibc_unlikely (next_char_idx >= mctx->input.bufs_len)
  1003. && mctx->input.bufs_len < mctx->input.len)
  1004. || (__glibc_unlikely (next_char_idx >= mctx->input.valid_len)
  1005. && mctx->input.valid_len < mctx->input.len))
  1006. {
  1007. err = extend_buffers (mctx, next_char_idx + 1);
  1008. if (__glibc_unlikely (err != REG_NOERROR))
  1009. {
  1010. DEBUG_ASSERT (err == REG_ESPACE);
  1011. return -2;
  1012. }
  1013. }
  1014. cur_state = transit_state (&err, mctx, cur_state);
  1015. if (mctx->state_log != NULL)
  1016. cur_state = merge_state_with_log (&err, mctx, cur_state);
  1017. if (cur_state == NULL)
  1018. {
  1019. /* Reached the invalid state or an error. Try to recover a valid
  1020. state using the state log, if available and if we have not
  1021. already found a valid (even if not the longest) match. */
  1022. if (__glibc_unlikely (err != REG_NOERROR))
  1023. return -2;
  1024. if (mctx->state_log == NULL
  1025. || (match && !fl_longest_match)
  1026. || (cur_state = find_recover_state (&err, mctx)) == NULL)
  1027. break;
  1028. }
  1029. if (__glibc_unlikely (at_init_state))
  1030. {
  1031. if (old_state == cur_state)
  1032. next_start_idx = next_char_idx;
  1033. else
  1034. at_init_state = false;
  1035. }
  1036. if (cur_state->halt)
  1037. {
  1038. /* Reached a halt state.
  1039. Check the halt state can satisfy the current context. */
  1040. if (!cur_state->has_constraint
  1041. || check_halt_state_context (mctx, cur_state,
  1042. re_string_cur_idx (&mctx->input)))
  1043. {
  1044. /* We found an appropriate halt state. */
  1045. match_last = re_string_cur_idx (&mctx->input);
  1046. match = 1;
  1047. /* We found a match, do not modify match_first below. */
  1048. p_match_first = NULL;
  1049. if (!fl_longest_match)
  1050. break;
  1051. }
  1052. }
  1053. }
  1054. if (p_match_first)
  1055. *p_match_first += next_start_idx;
  1056. return match_last;
  1057. }
  1058. /* Check NODE match the current context. */
  1059. static bool
  1060. check_halt_node_context (const re_dfa_t *dfa, Idx node, unsigned int context)
  1061. {
  1062. re_token_type_t type = dfa->nodes[node].type;
  1063. unsigned int constraint = dfa->nodes[node].constraint;
  1064. if (type != END_OF_RE)
  1065. return false;
  1066. if (!constraint)
  1067. return true;
  1068. if (NOT_SATISFY_NEXT_CONSTRAINT (constraint, context))
  1069. return false;
  1070. return true;
  1071. }
  1072. /* Check the halt state STATE match the current context.
  1073. Return 0 if not match, if the node, STATE has, is a halt node and
  1074. match the context, return the node. */
  1075. static Idx
  1076. check_halt_state_context (const re_match_context_t *mctx,
  1077. const re_dfastate_t *state, Idx idx)
  1078. {
  1079. Idx i;
  1080. unsigned int context;
  1081. DEBUG_ASSERT (state->halt);
  1082. context = re_string_context_at (&mctx->input, idx, mctx->eflags);
  1083. for (i = 0; i < state->nodes.nelem; ++i)
  1084. if (check_halt_node_context (mctx->dfa, state->nodes.elems[i], context))
  1085. return state->nodes.elems[i];
  1086. return 0;
  1087. }
  1088. /* Compute the next node to which "NFA" transit from NODE("NFA" is a NFA
  1089. corresponding to the DFA).
  1090. Return the destination node, and update EPS_VIA_NODES;
  1091. return -1 in case of errors. */
  1092. static Idx
  1093. proceed_next_node (const re_match_context_t *mctx, Idx nregs, regmatch_t *regs,
  1094. Idx *pidx, Idx node, re_node_set *eps_via_nodes,
  1095. struct re_fail_stack_t *fs)
  1096. {
  1097. const re_dfa_t *const dfa = mctx->dfa;
  1098. Idx i;
  1099. bool ok;
  1100. if (IS_EPSILON_NODE (dfa->nodes[node].type))
  1101. {
  1102. re_node_set *cur_nodes = &mctx->state_log[*pidx]->nodes;
  1103. re_node_set *edests = &dfa->edests[node];
  1104. Idx dest_node;
  1105. ok = re_node_set_insert (eps_via_nodes, node);
  1106. if (__glibc_unlikely (! ok))
  1107. return -2;
  1108. /* Pick up a valid destination, or return -1 if none
  1109. is found. */
  1110. for (dest_node = -1, i = 0; i < edests->nelem; ++i)
  1111. {
  1112. Idx candidate = edests->elems[i];
  1113. if (!re_node_set_contains (cur_nodes, candidate))
  1114. continue;
  1115. if (dest_node == -1)
  1116. dest_node = candidate;
  1117. else
  1118. {
  1119. /* In order to avoid infinite loop like "(a*)*", return the second
  1120. epsilon-transition if the first was already considered. */
  1121. if (re_node_set_contains (eps_via_nodes, dest_node))
  1122. return candidate;
  1123. /* Otherwise, push the second epsilon-transition on the fail stack. */
  1124. else if (fs != NULL
  1125. && push_fail_stack (fs, *pidx, candidate, nregs, regs,
  1126. eps_via_nodes))
  1127. return -2;
  1128. /* We know we are going to exit. */
  1129. break;
  1130. }
  1131. }
  1132. return dest_node;
  1133. }
  1134. else
  1135. {
  1136. Idx naccepted = 0;
  1137. re_token_type_t type = dfa->nodes[node].type;
  1138. #ifdef RE_ENABLE_I18N
  1139. if (dfa->nodes[node].accept_mb)
  1140. naccepted = check_node_accept_bytes (dfa, node, &mctx->input, *pidx);
  1141. else
  1142. #endif /* RE_ENABLE_I18N */
  1143. if (type == OP_BACK_REF)
  1144. {
  1145. Idx subexp_idx = dfa->nodes[node].opr.idx + 1;
  1146. if (subexp_idx < nregs)
  1147. naccepted = regs[subexp_idx].rm_eo - regs[subexp_idx].rm_so;
  1148. if (fs != NULL)
  1149. {
  1150. if (subexp_idx >= nregs
  1151. || regs[subexp_idx].rm_so == -1
  1152. || regs[subexp_idx].rm_eo == -1)
  1153. return -1;
  1154. else if (naccepted)
  1155. {
  1156. char *buf = (char *) re_string_get_buffer (&mctx->input);
  1157. if (mctx->input.valid_len - *pidx < naccepted
  1158. || (memcmp (buf + regs[subexp_idx].rm_so, buf + *pidx,
  1159. naccepted)
  1160. != 0))
  1161. return -1;
  1162. }
  1163. }
  1164. if (naccepted == 0)
  1165. {
  1166. Idx dest_node;
  1167. ok = re_node_set_insert (eps_via_nodes, node);
  1168. if (__glibc_unlikely (! ok))
  1169. return -2;
  1170. dest_node = dfa->edests[node].elems[0];
  1171. if (re_node_set_contains (&mctx->state_log[*pidx]->nodes,
  1172. dest_node))
  1173. return dest_node;
  1174. }
  1175. }
  1176. if (naccepted != 0
  1177. || check_node_accept (mctx, dfa->nodes + node, *pidx))
  1178. {
  1179. Idx dest_node = dfa->nexts[node];
  1180. *pidx = (naccepted == 0) ? *pidx + 1 : *pidx + naccepted;
  1181. if (fs && (*pidx > mctx->match_last || mctx->state_log[*pidx] == NULL
  1182. || !re_node_set_contains (&mctx->state_log[*pidx]->nodes,
  1183. dest_node)))
  1184. return -1;
  1185. re_node_set_empty (eps_via_nodes);
  1186. return dest_node;
  1187. }
  1188. }
  1189. return -1;
  1190. }
  1191. static reg_errcode_t
  1192. __attribute_warn_unused_result__
  1193. push_fail_stack (struct re_fail_stack_t *fs, Idx str_idx, Idx dest_node,
  1194. Idx nregs, regmatch_t *regs, re_node_set *eps_via_nodes)
  1195. {
  1196. reg_errcode_t err;
  1197. Idx num = fs->num++;
  1198. if (fs->num == fs->alloc)
  1199. {
  1200. struct re_fail_stack_ent_t *new_array;
  1201. new_array = re_realloc (fs->stack, struct re_fail_stack_ent_t,
  1202. fs->alloc * 2);
  1203. if (new_array == NULL)
  1204. return REG_ESPACE;
  1205. fs->alloc *= 2;
  1206. fs->stack = new_array;
  1207. }
  1208. fs->stack[num].idx = str_idx;
  1209. fs->stack[num].node = dest_node;
  1210. fs->stack[num].regs = re_malloc (regmatch_t, nregs);
  1211. if (fs->stack[num].regs == NULL)
  1212. return REG_ESPACE;
  1213. memcpy (fs->stack[num].regs, regs, sizeof (regmatch_t) * nregs);
  1214. err = re_node_set_init_copy (&fs->stack[num].eps_via_nodes, eps_via_nodes);
  1215. return err;
  1216. }
  1217. static Idx
  1218. pop_fail_stack (struct re_fail_stack_t *fs, Idx *pidx, Idx nregs,
  1219. regmatch_t *regs, re_node_set *eps_via_nodes)
  1220. {
  1221. Idx num = --fs->num;
  1222. DEBUG_ASSERT (num >= 0);
  1223. *pidx = fs->stack[num].idx;
  1224. memcpy (regs, fs->stack[num].regs, sizeof (regmatch_t) * nregs);
  1225. re_node_set_free (eps_via_nodes);
  1226. re_free (fs->stack[num].regs);
  1227. *eps_via_nodes = fs->stack[num].eps_via_nodes;
  1228. return fs->stack[num].node;
  1229. }
  1230. #define DYNARRAY_STRUCT regmatch_list
  1231. #define DYNARRAY_ELEMENT regmatch_t
  1232. #define DYNARRAY_PREFIX regmatch_list_
  1233. #include <malloc/dynarray-skeleton.c>
  1234. /* Set the positions where the subexpressions are starts/ends to registers
  1235. PMATCH.
  1236. Note: We assume that pmatch[0] is already set, and
  1237. pmatch[i].rm_so == pmatch[i].rm_eo == -1 for 0 < i < nmatch. */
  1238. static reg_errcode_t
  1239. __attribute_warn_unused_result__
  1240. set_regs (const regex_t *preg, const re_match_context_t *mctx, size_t nmatch,
  1241. regmatch_t *pmatch, bool fl_backtrack)
  1242. {
  1243. const re_dfa_t *dfa = preg->buffer;
  1244. Idx idx, cur_node;
  1245. re_node_set eps_via_nodes;
  1246. struct re_fail_stack_t *fs;
  1247. struct re_fail_stack_t fs_body = { 0, 2, NULL };
  1248. struct regmatch_list prev_match;
  1249. regmatch_list_init (&prev_match);
  1250. DEBUG_ASSERT (nmatch > 1);
  1251. DEBUG_ASSERT (mctx->state_log != NULL);
  1252. if (fl_backtrack)
  1253. {
  1254. fs = &fs_body;
  1255. fs->stack = re_malloc (struct re_fail_stack_ent_t, fs->alloc);
  1256. if (fs->stack == NULL)
  1257. return REG_ESPACE;
  1258. }
  1259. else
  1260. fs = NULL;
  1261. cur_node = dfa->init_node;
  1262. re_node_set_init_empty (&eps_via_nodes);
  1263. if (!regmatch_list_resize (&prev_match, nmatch))
  1264. {
  1265. regmatch_list_free (&prev_match);
  1266. free_fail_stack_return (fs);
  1267. return REG_ESPACE;
  1268. }
  1269. regmatch_t *prev_idx_match = regmatch_list_begin (&prev_match);
  1270. memcpy (prev_idx_match, pmatch, sizeof (regmatch_t) * nmatch);
  1271. for (idx = pmatch[0].rm_so; idx <= pmatch[0].rm_eo ;)
  1272. {
  1273. update_regs (dfa, pmatch, prev_idx_match, cur_node, idx, nmatch);
  1274. if (idx == pmatch[0].rm_eo && cur_node == mctx->last_node)
  1275. {
  1276. Idx reg_idx;
  1277. if (fs)
  1278. {
  1279. for (reg_idx = 0; reg_idx < nmatch; ++reg_idx)
  1280. if (pmatch[reg_idx].rm_so > -1 && pmatch[reg_idx].rm_eo == -1)
  1281. break;
  1282. if (reg_idx == nmatch)
  1283. {
  1284. re_node_set_free (&eps_via_nodes);
  1285. regmatch_list_free (&prev_match);
  1286. return free_fail_stack_return (fs);
  1287. }
  1288. cur_node = pop_fail_stack (fs, &idx, nmatch, pmatch,
  1289. &eps_via_nodes);
  1290. }
  1291. else
  1292. {
  1293. re_node_set_free (&eps_via_nodes);
  1294. regmatch_list_free (&prev_match);
  1295. return REG_NOERROR;
  1296. }
  1297. }
  1298. /* Proceed to next node. */
  1299. cur_node = proceed_next_node (mctx, nmatch, pmatch, &idx, cur_node,
  1300. &eps_via_nodes, fs);
  1301. if (__glibc_unlikely (cur_node < 0))
  1302. {
  1303. if (__glibc_unlikely (cur_node == -2))
  1304. {
  1305. re_node_set_free (&eps_via_nodes);
  1306. regmatch_list_free (&prev_match);
  1307. free_fail_stack_return (fs);
  1308. return REG_ESPACE;
  1309. }
  1310. if (fs)
  1311. cur_node = pop_fail_stack (fs, &idx, nmatch, pmatch,
  1312. &eps_via_nodes);
  1313. else
  1314. {
  1315. re_node_set_free (&eps_via_nodes);
  1316. regmatch_list_free (&prev_match);
  1317. return REG_NOMATCH;
  1318. }
  1319. }
  1320. }
  1321. re_node_set_free (&eps_via_nodes);
  1322. regmatch_list_free (&prev_match);
  1323. return free_fail_stack_return (fs);
  1324. }
  1325. static reg_errcode_t
  1326. free_fail_stack_return (struct re_fail_stack_t *fs)
  1327. {
  1328. if (fs)
  1329. {
  1330. Idx fs_idx;
  1331. for (fs_idx = 0; fs_idx < fs->num; ++fs_idx)
  1332. {
  1333. re_node_set_free (&fs->stack[fs_idx].eps_via_nodes);
  1334. re_free (fs->stack[fs_idx].regs);
  1335. }
  1336. re_free (fs->stack);
  1337. }
  1338. return REG_NOERROR;
  1339. }
  1340. static void
  1341. update_regs (const re_dfa_t *dfa, regmatch_t *pmatch,
  1342. regmatch_t *prev_idx_match, Idx cur_node, Idx cur_idx, Idx nmatch)
  1343. {
  1344. int type = dfa->nodes[cur_node].type;
  1345. if (type == OP_OPEN_SUBEXP)
  1346. {
  1347. Idx reg_num = dfa->nodes[cur_node].opr.idx + 1;
  1348. /* We are at the first node of this sub expression. */
  1349. if (reg_num < nmatch)
  1350. {
  1351. pmatch[reg_num].rm_so = cur_idx;
  1352. pmatch[reg_num].rm_eo = -1;
  1353. }
  1354. }
  1355. else if (type == OP_CLOSE_SUBEXP)
  1356. {
  1357. Idx reg_num = dfa->nodes[cur_node].opr.idx + 1;
  1358. if (reg_num < nmatch)
  1359. {
  1360. /* We are at the last node of this sub expression. */
  1361. if (pmatch[reg_num].rm_so < cur_idx)
  1362. {
  1363. pmatch[reg_num].rm_eo = cur_idx;
  1364. /* This is a non-empty match or we are not inside an optional
  1365. subexpression. Accept this right away. */
  1366. memcpy (prev_idx_match, pmatch, sizeof (regmatch_t) * nmatch);
  1367. }
  1368. else
  1369. {
  1370. if (dfa->nodes[cur_node].opt_subexp
  1371. && prev_idx_match[reg_num].rm_so != -1)
  1372. /* We transited through an empty match for an optional
  1373. subexpression, like (a?)*, and this is not the subexp's
  1374. first match. Copy back the old content of the registers
  1375. so that matches of an inner subexpression are undone as
  1376. well, like in ((a?))*. */
  1377. memcpy (pmatch, prev_idx_match, sizeof (regmatch_t) * nmatch);
  1378. else
  1379. /* We completed a subexpression, but it may be part of
  1380. an optional one, so do not update PREV_IDX_MATCH. */
  1381. pmatch[reg_num].rm_eo = cur_idx;
  1382. }
  1383. }
  1384. }
  1385. }
  1386. /* This function checks the STATE_LOG from the SCTX->last_str_idx to 0
  1387. and sift the nodes in each states according to the following rules.
  1388. Updated state_log will be wrote to STATE_LOG.
  1389. Rules: We throw away the Node 'a' in the STATE_LOG[STR_IDX] if...
  1390. 1. When STR_IDX == MATCH_LAST(the last index in the state_log):
  1391. If 'a' isn't the LAST_NODE and 'a' can't epsilon transit to
  1392. the LAST_NODE, we throw away the node 'a'.
  1393. 2. When 0 <= STR_IDX < MATCH_LAST and 'a' accepts
  1394. string 's' and transit to 'b':
  1395. i. If 'b' isn't in the STATE_LOG[STR_IDX+strlen('s')], we throw
  1396. away the node 'a'.
  1397. ii. If 'b' is in the STATE_LOG[STR_IDX+strlen('s')] but 'b' is
  1398. thrown away, we throw away the node 'a'.
  1399. 3. When 0 <= STR_IDX < MATCH_LAST and 'a' epsilon transit to 'b':
  1400. i. If 'b' isn't in the STATE_LOG[STR_IDX], we throw away the
  1401. node 'a'.
  1402. ii. If 'b' is in the STATE_LOG[STR_IDX] but 'b' is thrown away,
  1403. we throw away the node 'a'. */
  1404. #define STATE_NODE_CONTAINS(state,node) \
  1405. ((state) != NULL && re_node_set_contains (&(state)->nodes, node))
  1406. static reg_errcode_t
  1407. sift_states_backward (const re_match_context_t *mctx, re_sift_context_t *sctx)
  1408. {
  1409. reg_errcode_t err;
  1410. int null_cnt = 0;
  1411. Idx str_idx = sctx->last_str_idx;
  1412. re_node_set cur_dest;
  1413. DEBUG_ASSERT (mctx->state_log != NULL && mctx->state_log[str_idx] != NULL);
  1414. /* Build sifted state_log[str_idx]. It has the nodes which can epsilon
  1415. transit to the last_node and the last_node itself. */
  1416. err = re_node_set_init_1 (&cur_dest, sctx->last_node);
  1417. if (__glibc_unlikely (err != REG_NOERROR))
  1418. return err;
  1419. err = update_cur_sifted_state (mctx, sctx, str_idx, &cur_dest);
  1420. if (__glibc_unlikely (err != REG_NOERROR))
  1421. goto free_return;
  1422. /* Then check each states in the state_log. */
  1423. while (str_idx > 0)
  1424. {
  1425. /* Update counters. */
  1426. null_cnt = (sctx->sifted_states[str_idx] == NULL) ? null_cnt + 1 : 0;
  1427. if (null_cnt > mctx->max_mb_elem_len)
  1428. {
  1429. memset (sctx->sifted_states, '\0',
  1430. sizeof (re_dfastate_t *) * str_idx);
  1431. re_node_set_free (&cur_dest);
  1432. return REG_NOERROR;
  1433. }
  1434. re_node_set_empty (&cur_dest);
  1435. --str_idx;
  1436. if (mctx->state_log[str_idx])
  1437. {
  1438. err = build_sifted_states (mctx, sctx, str_idx, &cur_dest);
  1439. if (__glibc_unlikely (err != REG_NOERROR))
  1440. goto free_return;
  1441. }
  1442. /* Add all the nodes which satisfy the following conditions:
  1443. - It can epsilon transit to a node in CUR_DEST.
  1444. - It is in CUR_SRC.
  1445. And update state_log. */
  1446. err = update_cur_sifted_state (mctx, sctx, str_idx, &cur_dest);
  1447. if (__glibc_unlikely (err != REG_NOERROR))
  1448. goto free_return;
  1449. }
  1450. err = REG_NOERROR;
  1451. free_return:
  1452. re_node_set_free (&cur_dest);
  1453. return err;
  1454. }
  1455. static reg_errcode_t
  1456. __attribute_warn_unused_result__
  1457. build_sifted_states (const re_match_context_t *mctx, re_sift_context_t *sctx,
  1458. Idx str_idx, re_node_set *cur_dest)
  1459. {
  1460. const re_dfa_t *const dfa = mctx->dfa;
  1461. const re_node_set *cur_src = &mctx->state_log[str_idx]->non_eps_nodes;
  1462. Idx i;
  1463. /* Then build the next sifted state.
  1464. We build the next sifted state on 'cur_dest', and update
  1465. 'sifted_states[str_idx]' with 'cur_dest'.
  1466. Note:
  1467. 'cur_dest' is the sifted state from 'state_log[str_idx + 1]'.
  1468. 'cur_src' points the node_set of the old 'state_log[str_idx]'
  1469. (with the epsilon nodes pre-filtered out). */
  1470. for (i = 0; i < cur_src->nelem; i++)
  1471. {
  1472. Idx prev_node = cur_src->elems[i];
  1473. int naccepted = 0;
  1474. bool ok;
  1475. DEBUG_ASSERT (!IS_EPSILON_NODE (dfa->nodes[prev_node].type));
  1476. #ifdef RE_ENABLE_I18N
  1477. /* If the node may accept "multi byte". */
  1478. if (dfa->nodes[prev_node].accept_mb)
  1479. naccepted = sift_states_iter_mb (mctx, sctx, prev_node,
  1480. str_idx, sctx->last_str_idx);
  1481. #endif /* RE_ENABLE_I18N */
  1482. /* We don't check backreferences here.
  1483. See update_cur_sifted_state(). */
  1484. if (!naccepted
  1485. && check_node_accept (mctx, dfa->nodes + prev_node, str_idx)
  1486. && STATE_NODE_CONTAINS (sctx->sifted_states[str_idx + 1],
  1487. dfa->nexts[prev_node]))
  1488. naccepted = 1;
  1489. if (naccepted == 0)
  1490. continue;
  1491. if (sctx->limits.nelem)
  1492. {
  1493. Idx to_idx = str_idx + naccepted;
  1494. if (check_dst_limits (mctx, &sctx->limits,
  1495. dfa->nexts[prev_node], to_idx,
  1496. prev_node, str_idx))
  1497. continue;
  1498. }
  1499. ok = re_node_set_insert (cur_dest, prev_node);
  1500. if (__glibc_unlikely (! ok))
  1501. return REG_ESPACE;
  1502. }
  1503. return REG_NOERROR;
  1504. }
  1505. /* Helper functions. */
  1506. static reg_errcode_t
  1507. clean_state_log_if_needed (re_match_context_t *mctx, Idx next_state_log_idx)
  1508. {
  1509. Idx top = mctx->state_log_top;
  1510. if ((next_state_log_idx >= mctx->input.bufs_len
  1511. && mctx->input.bufs_len < mctx->input.len)
  1512. || (next_state_log_idx >= mctx->input.valid_len
  1513. && mctx->input.valid_len < mctx->input.len))
  1514. {
  1515. reg_errcode_t err;
  1516. err = extend_buffers (mctx, next_state_log_idx + 1);
  1517. if (__glibc_unlikely (err != REG_NOERROR))
  1518. return err;
  1519. }
  1520. if (top < next_state_log_idx)
  1521. {
  1522. memset (mctx->state_log + top + 1, '\0',
  1523. sizeof (re_dfastate_t *) * (next_state_log_idx - top));
  1524. mctx->state_log_top = next_state_log_idx;
  1525. }
  1526. return REG_NOERROR;
  1527. }
  1528. static reg_errcode_t
  1529. merge_state_array (const re_dfa_t *dfa, re_dfastate_t **dst,
  1530. re_dfastate_t **src, Idx num)
  1531. {
  1532. Idx st_idx;
  1533. reg_errcode_t err;
  1534. for (st_idx = 0; st_idx < num; ++st_idx)
  1535. {
  1536. if (dst[st_idx] == NULL)
  1537. dst[st_idx] = src[st_idx];
  1538. else if (src[st_idx] != NULL)
  1539. {
  1540. re_node_set merged_set;
  1541. err = re_node_set_init_union (&merged_set, &dst[st_idx]->nodes,
  1542. &src[st_idx]->nodes);
  1543. if (__glibc_unlikely (err != REG_NOERROR))
  1544. return err;
  1545. dst[st_idx] = re_acquire_state (&err, dfa, &merged_set);
  1546. re_node_set_free (&merged_set);
  1547. if (__glibc_unlikely (err != REG_NOERROR))
  1548. return err;
  1549. }
  1550. }
  1551. return REG_NOERROR;
  1552. }
  1553. static reg_errcode_t
  1554. update_cur_sifted_state (const re_match_context_t *mctx,
  1555. re_sift_context_t *sctx, Idx str_idx,
  1556. re_node_set *dest_nodes)
  1557. {
  1558. const re_dfa_t *const dfa = mctx->dfa;
  1559. reg_errcode_t err = REG_NOERROR;
  1560. const re_node_set *candidates;
  1561. candidates = ((mctx->state_log[str_idx] == NULL) ? NULL
  1562. : &mctx->state_log[str_idx]->nodes);
  1563. if (dest_nodes->nelem == 0)
  1564. sctx->sifted_states[str_idx] = NULL;
  1565. else
  1566. {
  1567. if (candidates)
  1568. {
  1569. /* At first, add the nodes which can epsilon transit to a node in
  1570. DEST_NODE. */
  1571. err = add_epsilon_src_nodes (dfa, dest_nodes, candidates);
  1572. if (__glibc_unlikely (err != REG_NOERROR))
  1573. return err;
  1574. /* Then, check the limitations in the current sift_context. */
  1575. if (sctx->limits.nelem)
  1576. {
  1577. err = check_subexp_limits (dfa, dest_nodes, candidates, &sctx->limits,
  1578. mctx->bkref_ents, str_idx);
  1579. if (__glibc_unlikely (err != REG_NOERROR))
  1580. return err;
  1581. }
  1582. }
  1583. sctx->sifted_states[str_idx] = re_acquire_state (&err, dfa, dest_nodes);
  1584. if (__glibc_unlikely (err != REG_NOERROR))
  1585. return err;
  1586. }
  1587. if (candidates && mctx->state_log[str_idx]->has_backref)
  1588. {
  1589. err = sift_states_bkref (mctx, sctx, str_idx, candidates);
  1590. if (__glibc_unlikely (err != REG_NOERROR))
  1591. return err;
  1592. }
  1593. return REG_NOERROR;
  1594. }
  1595. static reg_errcode_t
  1596. __attribute_warn_unused_result__
  1597. add_epsilon_src_nodes (const re_dfa_t *dfa, re_node_set *dest_nodes,
  1598. const re_node_set *candidates)
  1599. {
  1600. reg_errcode_t err = REG_NOERROR;
  1601. Idx i;
  1602. re_dfastate_t *state = re_acquire_state (&err, dfa, dest_nodes);
  1603. if (__glibc_unlikely (err != REG_NOERROR))
  1604. return err;
  1605. if (!state->inveclosure.alloc)
  1606. {
  1607. err = re_node_set_alloc (&state->inveclosure, dest_nodes->nelem);
  1608. if (__glibc_unlikely (err != REG_NOERROR))
  1609. return REG_ESPACE;
  1610. for (i = 0; i < dest_nodes->nelem; i++)
  1611. {
  1612. err = re_node_set_merge (&state->inveclosure,
  1613. dfa->inveclosures + dest_nodes->elems[i]);
  1614. if (__glibc_unlikely (err != REG_NOERROR))
  1615. return REG_ESPACE;
  1616. }
  1617. }
  1618. return re_node_set_add_intersect (dest_nodes, candidates,
  1619. &state->inveclosure);
  1620. }
  1621. static reg_errcode_t
  1622. sub_epsilon_src_nodes (const re_dfa_t *dfa, Idx node, re_node_set *dest_nodes,
  1623. const re_node_set *candidates)
  1624. {
  1625. Idx ecl_idx;
  1626. reg_errcode_t err;
  1627. re_node_set *inv_eclosure = dfa->inveclosures + node;
  1628. re_node_set except_nodes;
  1629. re_node_set_init_empty (&except_nodes);
  1630. for (ecl_idx = 0; ecl_idx < inv_eclosure->nelem; ++ecl_idx)
  1631. {
  1632. Idx cur_node = inv_eclosure->elems[ecl_idx];
  1633. if (cur_node == node)
  1634. continue;
  1635. if (IS_EPSILON_NODE (dfa->nodes[cur_node].type))
  1636. {
  1637. Idx edst1 = dfa->edests[cur_node].elems[0];
  1638. Idx edst2 = ((dfa->edests[cur_node].nelem > 1)
  1639. ? dfa->edests[cur_node].elems[1] : -1);
  1640. if ((!re_node_set_contains (inv_eclosure, edst1)
  1641. && re_node_set_contains (dest_nodes, edst1))
  1642. || (edst2 > 0
  1643. && !re_node_set_contains (inv_eclosure, edst2)
  1644. && re_node_set_contains (dest_nodes, edst2)))
  1645. {
  1646. err = re_node_set_add_intersect (&except_nodes, candidates,
  1647. dfa->inveclosures + cur_node);
  1648. if (__glibc_unlikely (err != REG_NOERROR))
  1649. {
  1650. re_node_set_free (&except_nodes);
  1651. return err;
  1652. }
  1653. }
  1654. }
  1655. }
  1656. for (ecl_idx = 0; ecl_idx < inv_eclosure->nelem; ++ecl_idx)
  1657. {
  1658. Idx cur_node = inv_eclosure->elems[ecl_idx];
  1659. if (!re_node_set_contains (&except_nodes, cur_node))
  1660. {
  1661. Idx idx = re_node_set_contains (dest_nodes, cur_node) - 1;
  1662. re_node_set_remove_at (dest_nodes, idx);
  1663. }
  1664. }
  1665. re_node_set_free (&except_nodes);
  1666. return REG_NOERROR;
  1667. }
  1668. static bool
  1669. check_dst_limits (const re_match_context_t *mctx, const re_node_set *limits,
  1670. Idx dst_node, Idx dst_idx, Idx src_node, Idx src_idx)
  1671. {
  1672. const re_dfa_t *const dfa = mctx->dfa;
  1673. Idx lim_idx, src_pos, dst_pos;
  1674. Idx dst_bkref_idx = search_cur_bkref_entry (mctx, dst_idx);
  1675. Idx src_bkref_idx = search_cur_bkref_entry (mctx, src_idx);
  1676. for (lim_idx = 0; lim_idx < limits->nelem; ++lim_idx)
  1677. {
  1678. Idx subexp_idx;
  1679. struct re_backref_cache_entry *ent;
  1680. ent = mctx->bkref_ents + limits->elems[lim_idx];
  1681. subexp_idx = dfa->nodes[ent->node].opr.idx;
  1682. dst_pos = check_dst_limits_calc_pos (mctx, limits->elems[lim_idx],
  1683. subexp_idx, dst_node, dst_idx,
  1684. dst_bkref_idx);
  1685. src_pos = check_dst_limits_calc_pos (mctx, limits->elems[lim_idx],
  1686. subexp_idx, src_node, src_idx,
  1687. src_bkref_idx);
  1688. /* In case of:
  1689. <src> <dst> ( <subexp> )
  1690. ( <subexp> ) <src> <dst>
  1691. ( <subexp1> <src> <subexp2> <dst> <subexp3> ) */
  1692. if (src_pos == dst_pos)
  1693. continue; /* This is unrelated limitation. */
  1694. else
  1695. return true;
  1696. }
  1697. return false;
  1698. }
  1699. static int
  1700. check_dst_limits_calc_pos_1 (const re_match_context_t *mctx, int boundaries,
  1701. Idx subexp_idx, Idx from_node, Idx bkref_idx)
  1702. {
  1703. const re_dfa_t *const dfa = mctx->dfa;
  1704. const re_node_set *eclosures = dfa->eclosures + from_node;
  1705. Idx node_idx;
  1706. /* Else, we are on the boundary: examine the nodes on the epsilon
  1707. closure. */
  1708. for (node_idx = 0; node_idx < eclosures->nelem; ++node_idx)
  1709. {
  1710. Idx node = eclosures->elems[node_idx];
  1711. switch (dfa->nodes[node].type)
  1712. {
  1713. case OP_BACK_REF:
  1714. if (bkref_idx != -1)
  1715. {
  1716. struct re_backref_cache_entry *ent = mctx->bkref_ents + bkref_idx;
  1717. do
  1718. {
  1719. Idx dst;
  1720. int cpos;
  1721. if (ent->node != node)
  1722. continue;
  1723. if (subexp_idx < BITSET_WORD_BITS
  1724. && !(ent->eps_reachable_subexps_map
  1725. & ((bitset_word_t) 1 << subexp_idx)))
  1726. continue;
  1727. /* Recurse trying to reach the OP_OPEN_SUBEXP and
  1728. OP_CLOSE_SUBEXP cases below. But, if the
  1729. destination node is the same node as the source
  1730. node, don't recurse because it would cause an
  1731. infinite loop: a regex that exhibits this behavior
  1732. is ()\1*\1* */
  1733. dst = dfa->edests[node].elems[0];
  1734. if (dst == from_node)
  1735. {
  1736. if (boundaries & 1)
  1737. return -1;
  1738. else /* if (boundaries & 2) */
  1739. return 0;
  1740. }
  1741. cpos =
  1742. check_dst_limits_calc_pos_1 (mctx, boundaries, subexp_idx,
  1743. dst, bkref_idx);
  1744. if (cpos == -1 /* && (boundaries & 1) */)
  1745. return -1;
  1746. if (cpos == 0 && (boundaries & 2))
  1747. return 0;
  1748. if (subexp_idx < BITSET_WORD_BITS)
  1749. ent->eps_reachable_subexps_map
  1750. &= ~((bitset_word_t) 1 << subexp_idx);
  1751. }
  1752. while (ent++->more);
  1753. }
  1754. break;
  1755. case OP_OPEN_SUBEXP:
  1756. if ((boundaries & 1) && subexp_idx == dfa->nodes[node].opr.idx)
  1757. return -1;
  1758. break;
  1759. case OP_CLOSE_SUBEXP:
  1760. if ((boundaries & 2) && subexp_idx == dfa->nodes[node].opr.idx)
  1761. return 0;
  1762. break;
  1763. default:
  1764. break;
  1765. }
  1766. }
  1767. return (boundaries & 2) ? 1 : 0;
  1768. }
  1769. static int
  1770. check_dst_limits_calc_pos (const re_match_context_t *mctx, Idx limit,
  1771. Idx subexp_idx, Idx from_node, Idx str_idx,
  1772. Idx bkref_idx)
  1773. {
  1774. struct re_backref_cache_entry *lim = mctx->bkref_ents + limit;
  1775. int boundaries;
  1776. /* If we are outside the range of the subexpression, return -1 or 1. */
  1777. if (str_idx < lim->subexp_from)
  1778. return -1;
  1779. if (lim->subexp_to < str_idx)
  1780. return 1;
  1781. /* If we are within the subexpression, return 0. */
  1782. boundaries = (str_idx == lim->subexp_from);
  1783. boundaries |= (str_idx == lim->subexp_to) << 1;
  1784. if (boundaries == 0)
  1785. return 0;
  1786. /* Else, examine epsilon closure. */
  1787. return check_dst_limits_calc_pos_1 (mctx, boundaries, subexp_idx,
  1788. from_node, bkref_idx);
  1789. }
  1790. /* Check the limitations of sub expressions LIMITS, and remove the nodes
  1791. which are against limitations from DEST_NODES. */
  1792. static reg_errcode_t
  1793. check_subexp_limits (const re_dfa_t *dfa, re_node_set *dest_nodes,
  1794. const re_node_set *candidates, re_node_set *limits,
  1795. struct re_backref_cache_entry *bkref_ents, Idx str_idx)
  1796. {
  1797. reg_errcode_t err;
  1798. Idx node_idx, lim_idx;
  1799. for (lim_idx = 0; lim_idx < limits->nelem; ++lim_idx)
  1800. {
  1801. Idx subexp_idx;
  1802. struct re_backref_cache_entry *ent;
  1803. ent = bkref_ents + limits->elems[lim_idx];
  1804. if (str_idx <= ent->subexp_from || ent->str_idx < str_idx)
  1805. continue; /* This is unrelated limitation. */
  1806. subexp_idx = dfa->nodes[ent->node].opr.idx;
  1807. if (ent->subexp_to == str_idx)
  1808. {
  1809. Idx ops_node = -1;
  1810. Idx cls_node = -1;
  1811. for (node_idx = 0; node_idx < dest_nodes->nelem; ++node_idx)
  1812. {
  1813. Idx node = dest_nodes->elems[node_idx];
  1814. re_token_type_t type = dfa->nodes[node].type;
  1815. if (type == OP_OPEN_SUBEXP
  1816. && subexp_idx == dfa->nodes[node].opr.idx)
  1817. ops_node = node;
  1818. else if (type == OP_CLOSE_SUBEXP
  1819. && subexp_idx == dfa->nodes[node].opr.idx)
  1820. cls_node = node;
  1821. }
  1822. /* Check the limitation of the open subexpression. */
  1823. /* Note that (ent->subexp_to = str_idx != ent->subexp_from). */
  1824. if (ops_node >= 0)
  1825. {
  1826. err = sub_epsilon_src_nodes (dfa, ops_node, dest_nodes,
  1827. candidates);
  1828. if (__glibc_unlikely (err != REG_NOERROR))
  1829. return err;
  1830. }
  1831. /* Check the limitation of the close subexpression. */
  1832. if (cls_node >= 0)
  1833. for (node_idx = 0; node_idx < dest_nodes->nelem; ++node_idx)
  1834. {
  1835. Idx node = dest_nodes->elems[node_idx];
  1836. if (!re_node_set_contains (dfa->inveclosures + node,
  1837. cls_node)
  1838. && !re_node_set_contains (dfa->eclosures + node,
  1839. cls_node))
  1840. {
  1841. /* It is against this limitation.
  1842. Remove it form the current sifted state. */
  1843. err = sub_epsilon_src_nodes (dfa, node, dest_nodes,
  1844. candidates);
  1845. if (__glibc_unlikely (err != REG_NOERROR))
  1846. return err;
  1847. --node_idx;
  1848. }
  1849. }
  1850. }
  1851. else /* (ent->subexp_to != str_idx) */
  1852. {
  1853. for (node_idx = 0; node_idx < dest_nodes->nelem; ++node_idx)
  1854. {
  1855. Idx node = dest_nodes->elems[node_idx];
  1856. re_token_type_t type = dfa->nodes[node].type;
  1857. if (type == OP_CLOSE_SUBEXP || type == OP_OPEN_SUBEXP)
  1858. {
  1859. if (subexp_idx != dfa->nodes[node].opr.idx)
  1860. continue;
  1861. /* It is against this limitation.
  1862. Remove it form the current sifted state. */
  1863. err = sub_epsilon_src_nodes (dfa, node, dest_nodes,
  1864. candidates);
  1865. if (__glibc_unlikely (err != REG_NOERROR))
  1866. return err;
  1867. }
  1868. }
  1869. }
  1870. }
  1871. return REG_NOERROR;
  1872. }
  1873. static reg_errcode_t
  1874. __attribute_warn_unused_result__
  1875. sift_states_bkref (const re_match_context_t *mctx, re_sift_context_t *sctx,
  1876. Idx str_idx, const re_node_set *candidates)
  1877. {
  1878. const re_dfa_t *const dfa = mctx->dfa;
  1879. reg_errcode_t err;
  1880. Idx node_idx, node;
  1881. re_sift_context_t local_sctx;
  1882. Idx first_idx = search_cur_bkref_entry (mctx, str_idx);
  1883. if (first_idx == -1)
  1884. return REG_NOERROR;
  1885. local_sctx.sifted_states = NULL; /* Mark that it hasn't been initialized. */
  1886. for (node_idx = 0; node_idx < candidates->nelem; ++node_idx)
  1887. {
  1888. Idx enabled_idx;
  1889. re_token_type_t type;
  1890. struct re_backref_cache_entry *entry;
  1891. node = candidates->elems[node_idx];
  1892. type = dfa->nodes[node].type;
  1893. /* Avoid infinite loop for the REs like "()\1+". */
  1894. if (node == sctx->last_node && str_idx == sctx->last_str_idx)
  1895. continue;
  1896. if (type != OP_BACK_REF)
  1897. continue;
  1898. entry = mctx->bkref_ents + first_idx;
  1899. enabled_idx = first_idx;
  1900. do
  1901. {
  1902. Idx subexp_len;
  1903. Idx to_idx;
  1904. Idx dst_node;
  1905. bool ok;
  1906. re_dfastate_t *cur_state;
  1907. if (entry->node != node)
  1908. continue;
  1909. subexp_len = entry->subexp_to - entry->subexp_from;
  1910. to_idx = str_idx + subexp_len;
  1911. dst_node = (subexp_len ? dfa->nexts[node]
  1912. : dfa->edests[node].elems[0]);
  1913. if (to_idx > sctx->last_str_idx
  1914. || sctx->sifted_states[to_idx] == NULL
  1915. || !STATE_NODE_CONTAINS (sctx->sifted_states[to_idx], dst_node)
  1916. || check_dst_limits (mctx, &sctx->limits, node,
  1917. str_idx, dst_node, to_idx))
  1918. continue;
  1919. if (local_sctx.sifted_states == NULL)
  1920. {
  1921. local_sctx = *sctx;
  1922. err = re_node_set_init_copy (&local_sctx.limits, &sctx->limits);
  1923. if (__glibc_unlikely (err != REG_NOERROR))
  1924. goto free_return;
  1925. }
  1926. local_sctx.last_node = node;
  1927. local_sctx.last_str_idx = str_idx;
  1928. ok = re_node_set_insert (&local_sctx.limits, enabled_idx);
  1929. if (__glibc_unlikely (! ok))
  1930. {
  1931. err = REG_ESPACE;
  1932. goto free_return;
  1933. }
  1934. cur_state = local_sctx.sifted_states[str_idx];
  1935. err = sift_states_backward (mctx, &local_sctx);
  1936. if (__glibc_unlikely (err != REG_NOERROR))
  1937. goto free_return;
  1938. if (sctx->limited_states != NULL)
  1939. {
  1940. err = merge_state_array (dfa, sctx->limited_states,
  1941. local_sctx.sifted_states,
  1942. str_idx + 1);
  1943. if (__glibc_unlikely (err != REG_NOERROR))
  1944. goto free_return;
  1945. }
  1946. local_sctx.sifted_states[str_idx] = cur_state;
  1947. re_node_set_remove (&local_sctx.limits, enabled_idx);
  1948. /* mctx->bkref_ents may have changed, reload the pointer. */
  1949. entry = mctx->bkref_ents + enabled_idx;
  1950. }
  1951. while (enabled_idx++, entry++->more);
  1952. }
  1953. err = REG_NOERROR;
  1954. free_return:
  1955. if (local_sctx.sifted_states != NULL)
  1956. {
  1957. re_node_set_free (&local_sctx.limits);
  1958. }
  1959. return err;
  1960. }
  1961. #ifdef RE_ENABLE_I18N
  1962. static int
  1963. sift_states_iter_mb (const re_match_context_t *mctx, re_sift_context_t *sctx,
  1964. Idx node_idx, Idx str_idx, Idx max_str_idx)
  1965. {
  1966. const re_dfa_t *const dfa = mctx->dfa;
  1967. int naccepted;
  1968. /* Check the node can accept "multi byte". */
  1969. naccepted = check_node_accept_bytes (dfa, node_idx, &mctx->input, str_idx);
  1970. if (naccepted > 0 && str_idx + naccepted <= max_str_idx
  1971. && !STATE_NODE_CONTAINS (sctx->sifted_states[str_idx + naccepted],
  1972. dfa->nexts[node_idx]))
  1973. /* The node can't accept the "multi byte", or the
  1974. destination was already thrown away, then the node
  1975. couldn't accept the current input "multi byte". */
  1976. naccepted = 0;
  1977. /* Otherwise, it is sure that the node could accept
  1978. 'naccepted' bytes input. */
  1979. return naccepted;
  1980. }
  1981. #endif /* RE_ENABLE_I18N */
  1982. /* Functions for state transition. */
  1983. /* Return the next state to which the current state STATE will transit by
  1984. accepting the current input byte, and update STATE_LOG if necessary.
  1985. If STATE can accept a multibyte char/collating element/back reference
  1986. update the destination of STATE_LOG. */
  1987. static re_dfastate_t *
  1988. __attribute_warn_unused_result__
  1989. transit_state (reg_errcode_t *err, re_match_context_t *mctx,
  1990. re_dfastate_t *state)
  1991. {
  1992. re_dfastate_t **trtable;
  1993. unsigned char ch;
  1994. #ifdef RE_ENABLE_I18N
  1995. /* If the current state can accept multibyte. */
  1996. if (__glibc_unlikely (state->accept_mb))
  1997. {
  1998. *err = transit_state_mb (mctx, state);
  1999. if (__glibc_unlikely (*err != REG_NOERROR))
  2000. return NULL;
  2001. }
  2002. #endif /* RE_ENABLE_I18N */
  2003. /* Then decide the next state with the single byte. */
  2004. #if 0
  2005. if (0)
  2006. /* don't use transition table */
  2007. return transit_state_sb (err, mctx, state);
  2008. #endif
  2009. /* Use transition table */
  2010. ch = re_string_fetch_byte (&mctx->input);
  2011. for (;;)
  2012. {
  2013. trtable = state->trtable;
  2014. if (__glibc_likely (trtable != NULL))
  2015. return trtable[ch];
  2016. trtable = state->word_trtable;
  2017. if (__glibc_likely (trtable != NULL))
  2018. {
  2019. unsigned int context;
  2020. context
  2021. = re_string_context_at (&mctx->input,
  2022. re_string_cur_idx (&mctx->input) - 1,
  2023. mctx->eflags);
  2024. if (IS_WORD_CONTEXT (context))
  2025. return trtable[ch + SBC_MAX];
  2026. else
  2027. return trtable[ch];
  2028. }
  2029. if (!build_trtable (mctx->dfa, state))
  2030. {
  2031. *err = REG_ESPACE;
  2032. return NULL;
  2033. }
  2034. /* Retry, we now have a transition table. */
  2035. }
  2036. }
  2037. /* Update the state_log if we need */
  2038. static re_dfastate_t *
  2039. merge_state_with_log (reg_errcode_t *err, re_match_context_t *mctx,
  2040. re_dfastate_t *next_state)
  2041. {
  2042. const re_dfa_t *const dfa = mctx->dfa;
  2043. Idx cur_idx = re_string_cur_idx (&mctx->input);
  2044. if (cur_idx > mctx->state_log_top)
  2045. {
  2046. mctx->state_log[cur_idx] = next_state;
  2047. mctx->state_log_top = cur_idx;
  2048. }
  2049. else if (mctx->state_log[cur_idx] == 0)
  2050. {
  2051. mctx->state_log[cur_idx] = next_state;
  2052. }
  2053. else
  2054. {
  2055. re_dfastate_t *pstate;
  2056. unsigned int context;
  2057. re_node_set next_nodes, *log_nodes, *table_nodes = NULL;
  2058. /* If (state_log[cur_idx] != 0), it implies that cur_idx is
  2059. the destination of a multibyte char/collating element/
  2060. back reference. Then the next state is the union set of
  2061. these destinations and the results of the transition table. */
  2062. pstate = mctx->state_log[cur_idx];
  2063. log_nodes = pstate->entrance_nodes;
  2064. if (next_state != NULL)
  2065. {
  2066. table_nodes = next_state->entrance_nodes;
  2067. *err = re_node_set_init_union (&next_nodes, table_nodes,
  2068. log_nodes);
  2069. if (__glibc_unlikely (*err != REG_NOERROR))
  2070. return NULL;
  2071. }
  2072. else
  2073. next_nodes = *log_nodes;
  2074. /* Note: We already add the nodes of the initial state,
  2075. then we don't need to add them here. */
  2076. context = re_string_context_at (&mctx->input,
  2077. re_string_cur_idx (&mctx->input) - 1,
  2078. mctx->eflags);
  2079. next_state = mctx->state_log[cur_idx]
  2080. = re_acquire_state_context (err, dfa, &next_nodes, context);
  2081. /* We don't need to check errors here, since the return value of
  2082. this function is next_state and ERR is already set. */
  2083. if (table_nodes != NULL)
  2084. re_node_set_free (&next_nodes);
  2085. }
  2086. if (__glibc_unlikely (dfa->nbackref) && next_state != NULL)
  2087. {
  2088. /* Check OP_OPEN_SUBEXP in the current state in case that we use them
  2089. later. We must check them here, since the back references in the
  2090. next state might use them. */
  2091. *err = check_subexp_matching_top (mctx, &next_state->nodes,
  2092. cur_idx);
  2093. if (__glibc_unlikely (*err != REG_NOERROR))
  2094. return NULL;
  2095. /* If the next state has back references. */
  2096. if (next_state->has_backref)
  2097. {
  2098. *err = transit_state_bkref (mctx, &next_state->nodes);
  2099. if (__glibc_unlikely (*err != REG_NOERROR))
  2100. return NULL;
  2101. next_state = mctx->state_log[cur_idx];
  2102. }
  2103. }
  2104. return next_state;
  2105. }
  2106. /* Skip bytes in the input that correspond to part of a
  2107. multi-byte match, then look in the log for a state
  2108. from which to restart matching. */
  2109. static re_dfastate_t *
  2110. find_recover_state (reg_errcode_t *err, re_match_context_t *mctx)
  2111. {
  2112. re_dfastate_t *cur_state;
  2113. do
  2114. {
  2115. Idx max = mctx->state_log_top;
  2116. Idx cur_str_idx = re_string_cur_idx (&mctx->input);
  2117. do
  2118. {
  2119. if (++cur_str_idx > max)
  2120. return NULL;
  2121. re_string_skip_bytes (&mctx->input, 1);
  2122. }
  2123. while (mctx->state_log[cur_str_idx] == NULL);
  2124. cur_state = merge_state_with_log (err, mctx, NULL);
  2125. }
  2126. while (*err == REG_NOERROR && cur_state == NULL);
  2127. return cur_state;
  2128. }
  2129. /* Helper functions for transit_state. */
  2130. /* From the node set CUR_NODES, pick up the nodes whose types are
  2131. OP_OPEN_SUBEXP and which have corresponding back references in the regular
  2132. expression. And register them to use them later for evaluating the
  2133. corresponding back references. */
  2134. static reg_errcode_t
  2135. check_subexp_matching_top (re_match_context_t *mctx, re_node_set *cur_nodes,
  2136. Idx str_idx)
  2137. {
  2138. const re_dfa_t *const dfa = mctx->dfa;
  2139. Idx node_idx;
  2140. reg_errcode_t err;
  2141. /* TODO: This isn't efficient.
  2142. Because there might be more than one nodes whose types are
  2143. OP_OPEN_SUBEXP and whose index is SUBEXP_IDX, we must check all
  2144. nodes.
  2145. E.g. RE: (a){2} */
  2146. for (node_idx = 0; node_idx < cur_nodes->nelem; ++node_idx)
  2147. {
  2148. Idx node = cur_nodes->elems[node_idx];
  2149. if (dfa->nodes[node].type == OP_OPEN_SUBEXP
  2150. && dfa->nodes[node].opr.idx < BITSET_WORD_BITS
  2151. && (dfa->used_bkref_map
  2152. & ((bitset_word_t) 1 << dfa->nodes[node].opr.idx)))
  2153. {
  2154. err = match_ctx_add_subtop (mctx, node, str_idx);
  2155. if (__glibc_unlikely (err != REG_NOERROR))
  2156. return err;
  2157. }
  2158. }
  2159. return REG_NOERROR;
  2160. }
  2161. #if 0
  2162. /* Return the next state to which the current state STATE will transit by
  2163. accepting the current input byte. */
  2164. static re_dfastate_t *
  2165. transit_state_sb (reg_errcode_t *err, re_match_context_t *mctx,
  2166. re_dfastate_t *state)
  2167. {
  2168. const re_dfa_t *const dfa = mctx->dfa;
  2169. re_node_set next_nodes;
  2170. re_dfastate_t *next_state;
  2171. Idx node_cnt, cur_str_idx = re_string_cur_idx (&mctx->input);
  2172. unsigned int context;
  2173. *err = re_node_set_alloc (&next_nodes, state->nodes.nelem + 1);
  2174. if (__glibc_unlikely (*err != REG_NOERROR))
  2175. return NULL;
  2176. for (node_cnt = 0; node_cnt < state->nodes.nelem; ++node_cnt)
  2177. {
  2178. Idx cur_node = state->nodes.elems[node_cnt];
  2179. if (check_node_accept (mctx, dfa->nodes + cur_node, cur_str_idx))
  2180. {
  2181. *err = re_node_set_merge (&next_nodes,
  2182. dfa->eclosures + dfa->nexts[cur_node]);
  2183. if (__glibc_unlikely (*err != REG_NOERROR))
  2184. {
  2185. re_node_set_free (&next_nodes);
  2186. return NULL;
  2187. }
  2188. }
  2189. }
  2190. context = re_string_context_at (&mctx->input, cur_str_idx, mctx->eflags);
  2191. next_state = re_acquire_state_context (err, dfa, &next_nodes, context);
  2192. /* We don't need to check errors here, since the return value of
  2193. this function is next_state and ERR is already set. */
  2194. re_node_set_free (&next_nodes);
  2195. re_string_skip_bytes (&mctx->input, 1);
  2196. return next_state;
  2197. }
  2198. #endif
  2199. #ifdef RE_ENABLE_I18N
  2200. static reg_errcode_t
  2201. transit_state_mb (re_match_context_t *mctx, re_dfastate_t *pstate)
  2202. {
  2203. const re_dfa_t *const dfa = mctx->dfa;
  2204. reg_errcode_t err;
  2205. Idx i;
  2206. for (i = 0; i < pstate->nodes.nelem; ++i)
  2207. {
  2208. re_node_set dest_nodes, *new_nodes;
  2209. Idx cur_node_idx = pstate->nodes.elems[i];
  2210. int naccepted;
  2211. Idx dest_idx;
  2212. unsigned int context;
  2213. re_dfastate_t *dest_state;
  2214. if (!dfa->nodes[cur_node_idx].accept_mb)
  2215. continue;
  2216. if (dfa->nodes[cur_node_idx].constraint)
  2217. {
  2218. context = re_string_context_at (&mctx->input,
  2219. re_string_cur_idx (&mctx->input),
  2220. mctx->eflags);
  2221. if (NOT_SATISFY_NEXT_CONSTRAINT (dfa->nodes[cur_node_idx].constraint,
  2222. context))
  2223. continue;
  2224. }
  2225. /* How many bytes the node can accept? */
  2226. naccepted = check_node_accept_bytes (dfa, cur_node_idx, &mctx->input,
  2227. re_string_cur_idx (&mctx->input));
  2228. if (naccepted == 0)
  2229. continue;
  2230. /* The node can accepts 'naccepted' bytes. */
  2231. dest_idx = re_string_cur_idx (&mctx->input) + naccepted;
  2232. mctx->max_mb_elem_len = ((mctx->max_mb_elem_len < naccepted) ? naccepted
  2233. : mctx->max_mb_elem_len);
  2234. err = clean_state_log_if_needed (mctx, dest_idx);
  2235. if (__glibc_unlikely (err != REG_NOERROR))
  2236. return err;
  2237. DEBUG_ASSERT (dfa->nexts[cur_node_idx] != -1);
  2238. new_nodes = dfa->eclosures + dfa->nexts[cur_node_idx];
  2239. dest_state = mctx->state_log[dest_idx];
  2240. if (dest_state == NULL)
  2241. dest_nodes = *new_nodes;
  2242. else
  2243. {
  2244. err = re_node_set_init_union (&dest_nodes,
  2245. dest_state->entrance_nodes, new_nodes);
  2246. if (__glibc_unlikely (err != REG_NOERROR))
  2247. return err;
  2248. }
  2249. context = re_string_context_at (&mctx->input, dest_idx - 1,
  2250. mctx->eflags);
  2251. mctx->state_log[dest_idx]
  2252. = re_acquire_state_context (&err, dfa, &dest_nodes, context);
  2253. if (dest_state != NULL)
  2254. re_node_set_free (&dest_nodes);
  2255. if (__glibc_unlikely (mctx->state_log[dest_idx] == NULL
  2256. && err != REG_NOERROR))
  2257. return err;
  2258. }
  2259. return REG_NOERROR;
  2260. }
  2261. #endif /* RE_ENABLE_I18N */
  2262. static reg_errcode_t
  2263. transit_state_bkref (re_match_context_t *mctx, const re_node_set *nodes)
  2264. {
  2265. const re_dfa_t *const dfa = mctx->dfa;
  2266. reg_errcode_t err;
  2267. Idx i;
  2268. Idx cur_str_idx = re_string_cur_idx (&mctx->input);
  2269. for (i = 0; i < nodes->nelem; ++i)
  2270. {
  2271. Idx dest_str_idx, prev_nelem, bkc_idx;
  2272. Idx node_idx = nodes->elems[i];
  2273. unsigned int context;
  2274. const re_token_t *node = dfa->nodes + node_idx;
  2275. re_node_set *new_dest_nodes;
  2276. /* Check whether 'node' is a backreference or not. */
  2277. if (node->type != OP_BACK_REF)
  2278. continue;
  2279. if (node->constraint)
  2280. {
  2281. context = re_string_context_at (&mctx->input, cur_str_idx,
  2282. mctx->eflags);
  2283. if (NOT_SATISFY_NEXT_CONSTRAINT (node->constraint, context))
  2284. continue;
  2285. }
  2286. /* 'node' is a backreference.
  2287. Check the substring which the substring matched. */
  2288. bkc_idx = mctx->nbkref_ents;
  2289. err = get_subexp (mctx, node_idx, cur_str_idx);
  2290. if (__glibc_unlikely (err != REG_NOERROR))
  2291. goto free_return;
  2292. /* And add the epsilon closures (which is 'new_dest_nodes') of
  2293. the backreference to appropriate state_log. */
  2294. DEBUG_ASSERT (dfa->nexts[node_idx] != -1);
  2295. for (; bkc_idx < mctx->nbkref_ents; ++bkc_idx)
  2296. {
  2297. Idx subexp_len;
  2298. re_dfastate_t *dest_state;
  2299. struct re_backref_cache_entry *bkref_ent;
  2300. bkref_ent = mctx->bkref_ents + bkc_idx;
  2301. if (bkref_ent->node != node_idx || bkref_ent->str_idx != cur_str_idx)
  2302. continue;
  2303. subexp_len = bkref_ent->subexp_to - bkref_ent->subexp_from;
  2304. new_dest_nodes = (subexp_len == 0
  2305. ? dfa->eclosures + dfa->edests[node_idx].elems[0]
  2306. : dfa->eclosures + dfa->nexts[node_idx]);
  2307. dest_str_idx = (cur_str_idx + bkref_ent->subexp_to
  2308. - bkref_ent->subexp_from);
  2309. context = re_string_context_at (&mctx->input, dest_str_idx - 1,
  2310. mctx->eflags);
  2311. dest_state = mctx->state_log[dest_str_idx];
  2312. prev_nelem = ((mctx->state_log[cur_str_idx] == NULL) ? 0
  2313. : mctx->state_log[cur_str_idx]->nodes.nelem);
  2314. /* Add 'new_dest_node' to state_log. */
  2315. if (dest_state == NULL)
  2316. {
  2317. mctx->state_log[dest_str_idx]
  2318. = re_acquire_state_context (&err, dfa, new_dest_nodes,
  2319. context);
  2320. if (__glibc_unlikely (mctx->state_log[dest_str_idx] == NULL
  2321. && err != REG_NOERROR))
  2322. goto free_return;
  2323. }
  2324. else
  2325. {
  2326. re_node_set dest_nodes;
  2327. err = re_node_set_init_union (&dest_nodes,
  2328. dest_state->entrance_nodes,
  2329. new_dest_nodes);
  2330. if (__glibc_unlikely (err != REG_NOERROR))
  2331. {
  2332. re_node_set_free (&dest_nodes);
  2333. goto free_return;
  2334. }
  2335. mctx->state_log[dest_str_idx]
  2336. = re_acquire_state_context (&err, dfa, &dest_nodes, context);
  2337. re_node_set_free (&dest_nodes);
  2338. if (__glibc_unlikely (mctx->state_log[dest_str_idx] == NULL
  2339. && err != REG_NOERROR))
  2340. goto free_return;
  2341. }
  2342. /* We need to check recursively if the backreference can epsilon
  2343. transit. */
  2344. if (subexp_len == 0
  2345. && mctx->state_log[cur_str_idx]->nodes.nelem > prev_nelem)
  2346. {
  2347. err = check_subexp_matching_top (mctx, new_dest_nodes,
  2348. cur_str_idx);
  2349. if (__glibc_unlikely (err != REG_NOERROR))
  2350. goto free_return;
  2351. err = transit_state_bkref (mctx, new_dest_nodes);
  2352. if (__glibc_unlikely (err != REG_NOERROR))
  2353. goto free_return;
  2354. }
  2355. }
  2356. }
  2357. err = REG_NOERROR;
  2358. free_return:
  2359. return err;
  2360. }
  2361. /* Enumerate all the candidates which the backreference BKREF_NODE can match
  2362. at BKREF_STR_IDX, and register them by match_ctx_add_entry().
  2363. Note that we might collect inappropriate candidates here.
  2364. However, the cost of checking them strictly here is too high, then we
  2365. delay these checking for prune_impossible_nodes(). */
  2366. static reg_errcode_t
  2367. __attribute_warn_unused_result__
  2368. get_subexp (re_match_context_t *mctx, Idx bkref_node, Idx bkref_str_idx)
  2369. {
  2370. const re_dfa_t *const dfa = mctx->dfa;
  2371. Idx subexp_num, sub_top_idx;
  2372. const char *buf = (const char *) re_string_get_buffer (&mctx->input);
  2373. /* Return if we have already checked BKREF_NODE at BKREF_STR_IDX. */
  2374. Idx cache_idx = search_cur_bkref_entry (mctx, bkref_str_idx);
  2375. if (cache_idx != -1)
  2376. {
  2377. const struct re_backref_cache_entry *entry
  2378. = mctx->bkref_ents + cache_idx;
  2379. do
  2380. if (entry->node == bkref_node)
  2381. return REG_NOERROR; /* We already checked it. */
  2382. while (entry++->more);
  2383. }
  2384. subexp_num = dfa->nodes[bkref_node].opr.idx;
  2385. /* For each sub expression */
  2386. for (sub_top_idx = 0; sub_top_idx < mctx->nsub_tops; ++sub_top_idx)
  2387. {
  2388. reg_errcode_t err;
  2389. re_sub_match_top_t *sub_top = mctx->sub_tops[sub_top_idx];
  2390. re_sub_match_last_t *sub_last;
  2391. Idx sub_last_idx, sl_str, bkref_str_off;
  2392. if (dfa->nodes[sub_top->node].opr.idx != subexp_num)
  2393. continue; /* It isn't related. */
  2394. sl_str = sub_top->str_idx;
  2395. bkref_str_off = bkref_str_idx;
  2396. /* At first, check the last node of sub expressions we already
  2397. evaluated. */
  2398. for (sub_last_idx = 0; sub_last_idx < sub_top->nlasts; ++sub_last_idx)
  2399. {
  2400. regoff_t sl_str_diff;
  2401. sub_last = sub_top->lasts[sub_last_idx];
  2402. sl_str_diff = sub_last->str_idx - sl_str;
  2403. /* The matched string by the sub expression match with the substring
  2404. at the back reference? */
  2405. if (sl_str_diff > 0)
  2406. {
  2407. if (__glibc_unlikely (bkref_str_off + sl_str_diff
  2408. > mctx->input.valid_len))
  2409. {
  2410. /* Not enough chars for a successful match. */
  2411. if (bkref_str_off + sl_str_diff > mctx->input.len)
  2412. break;
  2413. err = clean_state_log_if_needed (mctx,
  2414. bkref_str_off
  2415. + sl_str_diff);
  2416. if (__glibc_unlikely (err != REG_NOERROR))
  2417. return err;
  2418. buf = (const char *) re_string_get_buffer (&mctx->input);
  2419. }
  2420. if (memcmp (buf + bkref_str_off, buf + sl_str, sl_str_diff) != 0)
  2421. /* We don't need to search this sub expression any more. */
  2422. break;
  2423. }
  2424. bkref_str_off += sl_str_diff;
  2425. sl_str += sl_str_diff;
  2426. err = get_subexp_sub (mctx, sub_top, sub_last, bkref_node,
  2427. bkref_str_idx);
  2428. /* Reload buf, since the preceding call might have reallocated
  2429. the buffer. */
  2430. buf = (const char *) re_string_get_buffer (&mctx->input);
  2431. if (err == REG_NOMATCH)
  2432. continue;
  2433. if (__glibc_unlikely (err != REG_NOERROR))
  2434. return err;
  2435. }
  2436. if (sub_last_idx < sub_top->nlasts)
  2437. continue;
  2438. if (sub_last_idx > 0)
  2439. ++sl_str;
  2440. /* Then, search for the other last nodes of the sub expression. */
  2441. for (; sl_str <= bkref_str_idx; ++sl_str)
  2442. {
  2443. Idx cls_node;
  2444. regoff_t sl_str_off;
  2445. const re_node_set *nodes;
  2446. sl_str_off = sl_str - sub_top->str_idx;
  2447. /* The matched string by the sub expression match with the substring
  2448. at the back reference? */
  2449. if (sl_str_off > 0)
  2450. {
  2451. if (__glibc_unlikely (bkref_str_off >= mctx->input.valid_len))
  2452. {
  2453. /* If we are at the end of the input, we cannot match. */
  2454. if (bkref_str_off >= mctx->input.len)
  2455. break;
  2456. err = extend_buffers (mctx, bkref_str_off + 1);
  2457. if (__glibc_unlikely (err != REG_NOERROR))
  2458. return err;
  2459. buf = (const char *) re_string_get_buffer (&mctx->input);
  2460. }
  2461. if (buf [bkref_str_off++] != buf[sl_str - 1])
  2462. break; /* We don't need to search this sub expression
  2463. any more. */
  2464. }
  2465. if (mctx->state_log[sl_str] == NULL)
  2466. continue;
  2467. /* Does this state have a ')' of the sub expression? */
  2468. nodes = &mctx->state_log[sl_str]->nodes;
  2469. cls_node = find_subexp_node (dfa, nodes, subexp_num,
  2470. OP_CLOSE_SUBEXP);
  2471. if (cls_node == -1)
  2472. continue; /* No. */
  2473. if (sub_top->path == NULL)
  2474. {
  2475. sub_top->path = calloc (sizeof (state_array_t),
  2476. sl_str - sub_top->str_idx + 1);
  2477. if (sub_top->path == NULL)
  2478. return REG_ESPACE;
  2479. }
  2480. /* Can the OP_OPEN_SUBEXP node arrive the OP_CLOSE_SUBEXP node
  2481. in the current context? */
  2482. err = check_arrival (mctx, sub_top->path, sub_top->node,
  2483. sub_top->str_idx, cls_node, sl_str,
  2484. OP_CLOSE_SUBEXP);
  2485. if (err == REG_NOMATCH)
  2486. continue;
  2487. if (__glibc_unlikely (err != REG_NOERROR))
  2488. return err;
  2489. sub_last = match_ctx_add_sublast (sub_top, cls_node, sl_str);
  2490. if (__glibc_unlikely (sub_last == NULL))
  2491. return REG_ESPACE;
  2492. err = get_subexp_sub (mctx, sub_top, sub_last, bkref_node,
  2493. bkref_str_idx);
  2494. buf = (const char *) re_string_get_buffer (&mctx->input);
  2495. if (err == REG_NOMATCH)
  2496. continue;
  2497. if (__glibc_unlikely (err != REG_NOERROR))
  2498. return err;
  2499. }
  2500. }
  2501. return REG_NOERROR;
  2502. }
  2503. /* Helper functions for get_subexp(). */
  2504. /* Check SUB_LAST can arrive to the back reference BKREF_NODE at BKREF_STR.
  2505. If it can arrive, register the sub expression expressed with SUB_TOP
  2506. and SUB_LAST. */
  2507. static reg_errcode_t
  2508. get_subexp_sub (re_match_context_t *mctx, const re_sub_match_top_t *sub_top,
  2509. re_sub_match_last_t *sub_last, Idx bkref_node, Idx bkref_str)
  2510. {
  2511. reg_errcode_t err;
  2512. Idx to_idx;
  2513. /* Can the subexpression arrive the back reference? */
  2514. err = check_arrival (mctx, &sub_last->path, sub_last->node,
  2515. sub_last->str_idx, bkref_node, bkref_str,
  2516. OP_OPEN_SUBEXP);
  2517. if (err != REG_NOERROR)
  2518. return err;
  2519. err = match_ctx_add_entry (mctx, bkref_node, bkref_str, sub_top->str_idx,
  2520. sub_last->str_idx);
  2521. if (__glibc_unlikely (err != REG_NOERROR))
  2522. return err;
  2523. to_idx = bkref_str + sub_last->str_idx - sub_top->str_idx;
  2524. return clean_state_log_if_needed (mctx, to_idx);
  2525. }
  2526. /* Find the first node which is '(' or ')' and whose index is SUBEXP_IDX.
  2527. Search '(' if FL_OPEN, or search ')' otherwise.
  2528. TODO: This function isn't efficient...
  2529. Because there might be more than one nodes whose types are
  2530. OP_OPEN_SUBEXP and whose index is SUBEXP_IDX, we must check all
  2531. nodes.
  2532. E.g. RE: (a){2} */
  2533. static Idx
  2534. find_subexp_node (const re_dfa_t *dfa, const re_node_set *nodes,
  2535. Idx subexp_idx, int type)
  2536. {
  2537. Idx cls_idx;
  2538. for (cls_idx = 0; cls_idx < nodes->nelem; ++cls_idx)
  2539. {
  2540. Idx cls_node = nodes->elems[cls_idx];
  2541. const re_token_t *node = dfa->nodes + cls_node;
  2542. if (node->type == type
  2543. && node->opr.idx == subexp_idx)
  2544. return cls_node;
  2545. }
  2546. return -1;
  2547. }
  2548. /* Check whether the node TOP_NODE at TOP_STR can arrive to the node
  2549. LAST_NODE at LAST_STR. We record the path onto PATH since it will be
  2550. heavily reused.
  2551. Return REG_NOERROR if it can arrive, or REG_NOMATCH otherwise. */
  2552. static reg_errcode_t
  2553. __attribute_warn_unused_result__
  2554. check_arrival (re_match_context_t *mctx, state_array_t *path, Idx top_node,
  2555. Idx top_str, Idx last_node, Idx last_str, int type)
  2556. {
  2557. const re_dfa_t *const dfa = mctx->dfa;
  2558. reg_errcode_t err = REG_NOERROR;
  2559. Idx subexp_num, backup_cur_idx, str_idx, null_cnt;
  2560. re_dfastate_t *cur_state = NULL;
  2561. re_node_set *cur_nodes, next_nodes;
  2562. re_dfastate_t **backup_state_log;
  2563. unsigned int context;
  2564. subexp_num = dfa->nodes[top_node].opr.idx;
  2565. /* Extend the buffer if we need. */
  2566. if (__glibc_unlikely (path->alloc < last_str + mctx->max_mb_elem_len + 1))
  2567. {
  2568. re_dfastate_t **new_array;
  2569. Idx old_alloc = path->alloc;
  2570. Idx incr_alloc = last_str + mctx->max_mb_elem_len + 1;
  2571. Idx new_alloc;
  2572. if (__glibc_unlikely (IDX_MAX - old_alloc < incr_alloc))
  2573. return REG_ESPACE;
  2574. new_alloc = old_alloc + incr_alloc;
  2575. if (__glibc_unlikely (SIZE_MAX / sizeof (re_dfastate_t *) < new_alloc))
  2576. return REG_ESPACE;
  2577. new_array = re_realloc (path->array, re_dfastate_t *, new_alloc);
  2578. if (__glibc_unlikely (new_array == NULL))
  2579. return REG_ESPACE;
  2580. path->array = new_array;
  2581. path->alloc = new_alloc;
  2582. memset (new_array + old_alloc, '\0',
  2583. sizeof (re_dfastate_t *) * (path->alloc - old_alloc));
  2584. }
  2585. str_idx = path->next_idx ? path->next_idx : top_str;
  2586. /* Temporary modify MCTX. */
  2587. backup_state_log = mctx->state_log;
  2588. backup_cur_idx = mctx->input.cur_idx;
  2589. mctx->state_log = path->array;
  2590. mctx->input.cur_idx = str_idx;
  2591. /* Setup initial node set. */
  2592. context = re_string_context_at (&mctx->input, str_idx - 1, mctx->eflags);
  2593. if (str_idx == top_str)
  2594. {
  2595. err = re_node_set_init_1 (&next_nodes, top_node);
  2596. if (__glibc_unlikely (err != REG_NOERROR))
  2597. return err;
  2598. err = check_arrival_expand_ecl (dfa, &next_nodes, subexp_num, type);
  2599. if (__glibc_unlikely (err != REG_NOERROR))
  2600. {
  2601. re_node_set_free (&next_nodes);
  2602. return err;
  2603. }
  2604. }
  2605. else
  2606. {
  2607. cur_state = mctx->state_log[str_idx];
  2608. if (cur_state && cur_state->has_backref)
  2609. {
  2610. err = re_node_set_init_copy (&next_nodes, &cur_state->nodes);
  2611. if (__glibc_unlikely (err != REG_NOERROR))
  2612. return err;
  2613. }
  2614. else
  2615. re_node_set_init_empty (&next_nodes);
  2616. }
  2617. if (str_idx == top_str || (cur_state && cur_state->has_backref))
  2618. {
  2619. if (next_nodes.nelem)
  2620. {
  2621. err = expand_bkref_cache (mctx, &next_nodes, str_idx,
  2622. subexp_num, type);
  2623. if (__glibc_unlikely (err != REG_NOERROR))
  2624. {
  2625. re_node_set_free (&next_nodes);
  2626. return err;
  2627. }
  2628. }
  2629. cur_state = re_acquire_state_context (&err, dfa, &next_nodes, context);
  2630. if (__glibc_unlikely (cur_state == NULL && err != REG_NOERROR))
  2631. {
  2632. re_node_set_free (&next_nodes);
  2633. return err;
  2634. }
  2635. mctx->state_log[str_idx] = cur_state;
  2636. }
  2637. for (null_cnt = 0; str_idx < last_str && null_cnt <= mctx->max_mb_elem_len;)
  2638. {
  2639. re_node_set_empty (&next_nodes);
  2640. if (mctx->state_log[str_idx + 1])
  2641. {
  2642. err = re_node_set_merge (&next_nodes,
  2643. &mctx->state_log[str_idx + 1]->nodes);
  2644. if (__glibc_unlikely (err != REG_NOERROR))
  2645. {
  2646. re_node_set_free (&next_nodes);
  2647. return err;
  2648. }
  2649. }
  2650. if (cur_state)
  2651. {
  2652. err = check_arrival_add_next_nodes (mctx, str_idx,
  2653. &cur_state->non_eps_nodes,
  2654. &next_nodes);
  2655. if (__glibc_unlikely (err != REG_NOERROR))
  2656. {
  2657. re_node_set_free (&next_nodes);
  2658. return err;
  2659. }
  2660. }
  2661. ++str_idx;
  2662. if (next_nodes.nelem)
  2663. {
  2664. err = check_arrival_expand_ecl (dfa, &next_nodes, subexp_num, type);
  2665. if (__glibc_unlikely (err != REG_NOERROR))
  2666. {
  2667. re_node_set_free (&next_nodes);
  2668. return err;
  2669. }
  2670. err = expand_bkref_cache (mctx, &next_nodes, str_idx,
  2671. subexp_num, type);
  2672. if (__glibc_unlikely (err != REG_NOERROR))
  2673. {
  2674. re_node_set_free (&next_nodes);
  2675. return err;
  2676. }
  2677. }
  2678. context = re_string_context_at (&mctx->input, str_idx - 1, mctx->eflags);
  2679. cur_state = re_acquire_state_context (&err, dfa, &next_nodes, context);
  2680. if (__glibc_unlikely (cur_state == NULL && err != REG_NOERROR))
  2681. {
  2682. re_node_set_free (&next_nodes);
  2683. return err;
  2684. }
  2685. mctx->state_log[str_idx] = cur_state;
  2686. null_cnt = cur_state == NULL ? null_cnt + 1 : 0;
  2687. }
  2688. re_node_set_free (&next_nodes);
  2689. cur_nodes = (mctx->state_log[last_str] == NULL ? NULL
  2690. : &mctx->state_log[last_str]->nodes);
  2691. path->next_idx = str_idx;
  2692. /* Fix MCTX. */
  2693. mctx->state_log = backup_state_log;
  2694. mctx->input.cur_idx = backup_cur_idx;
  2695. /* Then check the current node set has the node LAST_NODE. */
  2696. if (cur_nodes != NULL && re_node_set_contains (cur_nodes, last_node))
  2697. return REG_NOERROR;
  2698. return REG_NOMATCH;
  2699. }
  2700. /* Helper functions for check_arrival. */
  2701. /* Calculate the destination nodes of CUR_NODES at STR_IDX, and append them
  2702. to NEXT_NODES.
  2703. TODO: This function is similar to the functions transit_state*(),
  2704. however this function has many additional works.
  2705. Can't we unify them? */
  2706. static reg_errcode_t
  2707. __attribute_warn_unused_result__
  2708. check_arrival_add_next_nodes (re_match_context_t *mctx, Idx str_idx,
  2709. re_node_set *cur_nodes, re_node_set *next_nodes)
  2710. {
  2711. const re_dfa_t *const dfa = mctx->dfa;
  2712. bool ok;
  2713. Idx cur_idx;
  2714. #ifdef RE_ENABLE_I18N
  2715. reg_errcode_t err = REG_NOERROR;
  2716. #endif
  2717. re_node_set union_set;
  2718. re_node_set_init_empty (&union_set);
  2719. for (cur_idx = 0; cur_idx < cur_nodes->nelem; ++cur_idx)
  2720. {
  2721. int naccepted = 0;
  2722. Idx cur_node = cur_nodes->elems[cur_idx];
  2723. DEBUG_ASSERT (!IS_EPSILON_NODE (dfa->nodes[cur_node].type));
  2724. #ifdef RE_ENABLE_I18N
  2725. /* If the node may accept "multi byte". */
  2726. if (dfa->nodes[cur_node].accept_mb)
  2727. {
  2728. naccepted = check_node_accept_bytes (dfa, cur_node, &mctx->input,
  2729. str_idx);
  2730. if (naccepted > 1)
  2731. {
  2732. re_dfastate_t *dest_state;
  2733. Idx next_node = dfa->nexts[cur_node];
  2734. Idx next_idx = str_idx + naccepted;
  2735. dest_state = mctx->state_log[next_idx];
  2736. re_node_set_empty (&union_set);
  2737. if (dest_state)
  2738. {
  2739. err = re_node_set_merge (&union_set, &dest_state->nodes);
  2740. if (__glibc_unlikely (err != REG_NOERROR))
  2741. {
  2742. re_node_set_free (&union_set);
  2743. return err;
  2744. }
  2745. }
  2746. ok = re_node_set_insert (&union_set, next_node);
  2747. if (__glibc_unlikely (! ok))
  2748. {
  2749. re_node_set_free (&union_set);
  2750. return REG_ESPACE;
  2751. }
  2752. mctx->state_log[next_idx] = re_acquire_state (&err, dfa,
  2753. &union_set);
  2754. if (__glibc_unlikely (mctx->state_log[next_idx] == NULL
  2755. && err != REG_NOERROR))
  2756. {
  2757. re_node_set_free (&union_set);
  2758. return err;
  2759. }
  2760. }
  2761. }
  2762. #endif /* RE_ENABLE_I18N */
  2763. if (naccepted
  2764. || check_node_accept (mctx, dfa->nodes + cur_node, str_idx))
  2765. {
  2766. ok = re_node_set_insert (next_nodes, dfa->nexts[cur_node]);
  2767. if (__glibc_unlikely (! ok))
  2768. {
  2769. re_node_set_free (&union_set);
  2770. return REG_ESPACE;
  2771. }
  2772. }
  2773. }
  2774. re_node_set_free (&union_set);
  2775. return REG_NOERROR;
  2776. }
  2777. /* For all the nodes in CUR_NODES, add the epsilon closures of them to
  2778. CUR_NODES, however exclude the nodes which are:
  2779. - inside the sub expression whose number is EX_SUBEXP, if FL_OPEN.
  2780. - out of the sub expression whose number is EX_SUBEXP, if !FL_OPEN.
  2781. */
  2782. static reg_errcode_t
  2783. check_arrival_expand_ecl (const re_dfa_t *dfa, re_node_set *cur_nodes,
  2784. Idx ex_subexp, int type)
  2785. {
  2786. reg_errcode_t err;
  2787. Idx idx, outside_node;
  2788. re_node_set new_nodes;
  2789. DEBUG_ASSERT (cur_nodes->nelem);
  2790. err = re_node_set_alloc (&new_nodes, cur_nodes->nelem);
  2791. if (__glibc_unlikely (err != REG_NOERROR))
  2792. return err;
  2793. /* Create a new node set NEW_NODES with the nodes which are epsilon
  2794. closures of the node in CUR_NODES. */
  2795. for (idx = 0; idx < cur_nodes->nelem; ++idx)
  2796. {
  2797. Idx cur_node = cur_nodes->elems[idx];
  2798. const re_node_set *eclosure = dfa->eclosures + cur_node;
  2799. outside_node = find_subexp_node (dfa, eclosure, ex_subexp, type);
  2800. if (outside_node == -1)
  2801. {
  2802. /* There are no problematic nodes, just merge them. */
  2803. err = re_node_set_merge (&new_nodes, eclosure);
  2804. if (__glibc_unlikely (err != REG_NOERROR))
  2805. {
  2806. re_node_set_free (&new_nodes);
  2807. return err;
  2808. }
  2809. }
  2810. else
  2811. {
  2812. /* There are problematic nodes, re-calculate incrementally. */
  2813. err = check_arrival_expand_ecl_sub (dfa, &new_nodes, cur_node,
  2814. ex_subexp, type);
  2815. if (__glibc_unlikely (err != REG_NOERROR))
  2816. {
  2817. re_node_set_free (&new_nodes);
  2818. return err;
  2819. }
  2820. }
  2821. }
  2822. re_node_set_free (cur_nodes);
  2823. *cur_nodes = new_nodes;
  2824. return REG_NOERROR;
  2825. }
  2826. /* Helper function for check_arrival_expand_ecl.
  2827. Check incrementally the epsilon closure of TARGET, and if it isn't
  2828. problematic append it to DST_NODES. */
  2829. static reg_errcode_t
  2830. __attribute_warn_unused_result__
  2831. check_arrival_expand_ecl_sub (const re_dfa_t *dfa, re_node_set *dst_nodes,
  2832. Idx target, Idx ex_subexp, int type)
  2833. {
  2834. Idx cur_node;
  2835. for (cur_node = target; !re_node_set_contains (dst_nodes, cur_node);)
  2836. {
  2837. bool ok;
  2838. if (dfa->nodes[cur_node].type == type
  2839. && dfa->nodes[cur_node].opr.idx == ex_subexp)
  2840. {
  2841. if (type == OP_CLOSE_SUBEXP)
  2842. {
  2843. ok = re_node_set_insert (dst_nodes, cur_node);
  2844. if (__glibc_unlikely (! ok))
  2845. return REG_ESPACE;
  2846. }
  2847. break;
  2848. }
  2849. ok = re_node_set_insert (dst_nodes, cur_node);
  2850. if (__glibc_unlikely (! ok))
  2851. return REG_ESPACE;
  2852. if (dfa->edests[cur_node].nelem == 0)
  2853. break;
  2854. if (dfa->edests[cur_node].nelem == 2)
  2855. {
  2856. reg_errcode_t err;
  2857. err = check_arrival_expand_ecl_sub (dfa, dst_nodes,
  2858. dfa->edests[cur_node].elems[1],
  2859. ex_subexp, type);
  2860. if (__glibc_unlikely (err != REG_NOERROR))
  2861. return err;
  2862. }
  2863. cur_node = dfa->edests[cur_node].elems[0];
  2864. }
  2865. return REG_NOERROR;
  2866. }
  2867. /* For all the back references in the current state, calculate the
  2868. destination of the back references by the appropriate entry
  2869. in MCTX->BKREF_ENTS. */
  2870. static reg_errcode_t
  2871. __attribute_warn_unused_result__
  2872. expand_bkref_cache (re_match_context_t *mctx, re_node_set *cur_nodes,
  2873. Idx cur_str, Idx subexp_num, int type)
  2874. {
  2875. const re_dfa_t *const dfa = mctx->dfa;
  2876. reg_errcode_t err;
  2877. Idx cache_idx_start = search_cur_bkref_entry (mctx, cur_str);
  2878. struct re_backref_cache_entry *ent;
  2879. if (cache_idx_start == -1)
  2880. return REG_NOERROR;
  2881. restart:
  2882. ent = mctx->bkref_ents + cache_idx_start;
  2883. do
  2884. {
  2885. Idx to_idx, next_node;
  2886. /* Is this entry ENT is appropriate? */
  2887. if (!re_node_set_contains (cur_nodes, ent->node))
  2888. continue; /* No. */
  2889. to_idx = cur_str + ent->subexp_to - ent->subexp_from;
  2890. /* Calculate the destination of the back reference, and append it
  2891. to MCTX->STATE_LOG. */
  2892. if (to_idx == cur_str)
  2893. {
  2894. /* The backreference did epsilon transit, we must re-check all the
  2895. node in the current state. */
  2896. re_node_set new_dests;
  2897. reg_errcode_t err2, err3;
  2898. next_node = dfa->edests[ent->node].elems[0];
  2899. if (re_node_set_contains (cur_nodes, next_node))
  2900. continue;
  2901. err = re_node_set_init_1 (&new_dests, next_node);
  2902. err2 = check_arrival_expand_ecl (dfa, &new_dests, subexp_num, type);
  2903. err3 = re_node_set_merge (cur_nodes, &new_dests);
  2904. re_node_set_free (&new_dests);
  2905. if (__glibc_unlikely (err != REG_NOERROR || err2 != REG_NOERROR
  2906. || err3 != REG_NOERROR))
  2907. {
  2908. err = (err != REG_NOERROR ? err
  2909. : (err2 != REG_NOERROR ? err2 : err3));
  2910. return err;
  2911. }
  2912. /* TODO: It is still inefficient... */
  2913. goto restart;
  2914. }
  2915. else
  2916. {
  2917. re_node_set union_set;
  2918. next_node = dfa->nexts[ent->node];
  2919. if (mctx->state_log[to_idx])
  2920. {
  2921. bool ok;
  2922. if (re_node_set_contains (&mctx->state_log[to_idx]->nodes,
  2923. next_node))
  2924. continue;
  2925. err = re_node_set_init_copy (&union_set,
  2926. &mctx->state_log[to_idx]->nodes);
  2927. ok = re_node_set_insert (&union_set, next_node);
  2928. if (__glibc_unlikely (err != REG_NOERROR || ! ok))
  2929. {
  2930. re_node_set_free (&union_set);
  2931. err = err != REG_NOERROR ? err : REG_ESPACE;
  2932. return err;
  2933. }
  2934. }
  2935. else
  2936. {
  2937. err = re_node_set_init_1 (&union_set, next_node);
  2938. if (__glibc_unlikely (err != REG_NOERROR))
  2939. return err;
  2940. }
  2941. mctx->state_log[to_idx] = re_acquire_state (&err, dfa, &union_set);
  2942. re_node_set_free (&union_set);
  2943. if (__glibc_unlikely (mctx->state_log[to_idx] == NULL
  2944. && err != REG_NOERROR))
  2945. return err;
  2946. }
  2947. }
  2948. while (ent++->more);
  2949. return REG_NOERROR;
  2950. }
  2951. /* Build transition table for the state.
  2952. Return true if successful. */
  2953. static bool __attribute_noinline__
  2954. build_trtable (const re_dfa_t *dfa, re_dfastate_t *state)
  2955. {
  2956. reg_errcode_t err;
  2957. Idx i, j;
  2958. int ch;
  2959. bool need_word_trtable = false;
  2960. bitset_word_t elem, mask;
  2961. Idx ndests; /* Number of the destination states from 'state'. */
  2962. re_dfastate_t **trtable;
  2963. re_dfastate_t *dest_states[SBC_MAX];
  2964. re_dfastate_t *dest_states_word[SBC_MAX];
  2965. re_dfastate_t *dest_states_nl[SBC_MAX];
  2966. re_node_set follows;
  2967. bitset_t acceptable;
  2968. /* We build DFA states which corresponds to the destination nodes
  2969. from 'state'. 'dests_node[i]' represents the nodes which i-th
  2970. destination state contains, and 'dests_ch[i]' represents the
  2971. characters which i-th destination state accepts. */
  2972. re_node_set dests_node[SBC_MAX];
  2973. bitset_t dests_ch[SBC_MAX];
  2974. /* Initialize transition table. */
  2975. state->word_trtable = state->trtable = NULL;
  2976. /* At first, group all nodes belonging to 'state' into several
  2977. destinations. */
  2978. ndests = group_nodes_into_DFAstates (dfa, state, dests_node, dests_ch);
  2979. if (__glibc_unlikely (ndests <= 0))
  2980. {
  2981. /* Return false in case of an error, true otherwise. */
  2982. if (ndests == 0)
  2983. {
  2984. state->trtable = (re_dfastate_t **)
  2985. calloc (sizeof (re_dfastate_t *), SBC_MAX);
  2986. if (__glibc_unlikely (state->trtable == NULL))
  2987. return false;
  2988. return true;
  2989. }
  2990. return false;
  2991. }
  2992. err = re_node_set_alloc (&follows, ndests + 1);
  2993. if (__glibc_unlikely (err != REG_NOERROR))
  2994. {
  2995. out_free:
  2996. re_node_set_free (&follows);
  2997. for (i = 0; i < ndests; ++i)
  2998. re_node_set_free (dests_node + i);
  2999. return false;
  3000. }
  3001. bitset_empty (acceptable);
  3002. /* Then build the states for all destinations. */
  3003. for (i = 0; i < ndests; ++i)
  3004. {
  3005. Idx next_node;
  3006. re_node_set_empty (&follows);
  3007. /* Merge the follows of this destination states. */
  3008. for (j = 0; j < dests_node[i].nelem; ++j)
  3009. {
  3010. next_node = dfa->nexts[dests_node[i].elems[j]];
  3011. if (next_node != -1)
  3012. {
  3013. err = re_node_set_merge (&follows, dfa->eclosures + next_node);
  3014. if (__glibc_unlikely (err != REG_NOERROR))
  3015. goto out_free;
  3016. }
  3017. }
  3018. dest_states[i] = re_acquire_state_context (&err, dfa, &follows, 0);
  3019. if (__glibc_unlikely (dest_states[i] == NULL && err != REG_NOERROR))
  3020. goto out_free;
  3021. /* If the new state has context constraint,
  3022. build appropriate states for these contexts. */
  3023. if (dest_states[i]->has_constraint)
  3024. {
  3025. dest_states_word[i] = re_acquire_state_context (&err, dfa, &follows,
  3026. CONTEXT_WORD);
  3027. if (__glibc_unlikely (dest_states_word[i] == NULL
  3028. && err != REG_NOERROR))
  3029. goto out_free;
  3030. if (dest_states[i] != dest_states_word[i] && dfa->mb_cur_max > 1)
  3031. need_word_trtable = true;
  3032. dest_states_nl[i] = re_acquire_state_context (&err, dfa, &follows,
  3033. CONTEXT_NEWLINE);
  3034. if (__glibc_unlikely (dest_states_nl[i] == NULL && err != REG_NOERROR))
  3035. goto out_free;
  3036. }
  3037. else
  3038. {
  3039. dest_states_word[i] = dest_states[i];
  3040. dest_states_nl[i] = dest_states[i];
  3041. }
  3042. bitset_merge (acceptable, dests_ch[i]);
  3043. }
  3044. if (!__glibc_unlikely (need_word_trtable))
  3045. {
  3046. /* We don't care about whether the following character is a word
  3047. character, or we are in a single-byte character set so we can
  3048. discern by looking at the character code: allocate a
  3049. 256-entry transition table. */
  3050. trtable = state->trtable =
  3051. (re_dfastate_t **) calloc (sizeof (re_dfastate_t *), SBC_MAX);
  3052. if (__glibc_unlikely (trtable == NULL))
  3053. goto out_free;
  3054. /* For all characters ch...: */
  3055. for (i = 0; i < BITSET_WORDS; ++i)
  3056. for (ch = i * BITSET_WORD_BITS, elem = acceptable[i], mask = 1;
  3057. elem;
  3058. mask <<= 1, elem >>= 1, ++ch)
  3059. if (__glibc_unlikely (elem & 1))
  3060. {
  3061. /* There must be exactly one destination which accepts
  3062. character ch. See group_nodes_into_DFAstates. */
  3063. for (j = 0; (dests_ch[j][i] & mask) == 0; ++j)
  3064. ;
  3065. /* j-th destination accepts the word character ch. */
  3066. if (dfa->word_char[i] & mask)
  3067. trtable[ch] = dest_states_word[j];
  3068. else
  3069. trtable[ch] = dest_states[j];
  3070. }
  3071. }
  3072. else
  3073. {
  3074. /* We care about whether the following character is a word
  3075. character, and we are in a multi-byte character set: discern
  3076. by looking at the character code: build two 256-entry
  3077. transition tables, one starting at trtable[0] and one
  3078. starting at trtable[SBC_MAX]. */
  3079. trtable = state->word_trtable =
  3080. (re_dfastate_t **) calloc (sizeof (re_dfastate_t *), 2 * SBC_MAX);
  3081. if (__glibc_unlikely (trtable == NULL))
  3082. goto out_free;
  3083. /* For all characters ch...: */
  3084. for (i = 0; i < BITSET_WORDS; ++i)
  3085. for (ch = i * BITSET_WORD_BITS, elem = acceptable[i], mask = 1;
  3086. elem;
  3087. mask <<= 1, elem >>= 1, ++ch)
  3088. if (__glibc_unlikely (elem & 1))
  3089. {
  3090. /* There must be exactly one destination which accepts
  3091. character ch. See group_nodes_into_DFAstates. */
  3092. for (j = 0; (dests_ch[j][i] & mask) == 0; ++j)
  3093. ;
  3094. /* j-th destination accepts the word character ch. */
  3095. trtable[ch] = dest_states[j];
  3096. trtable[ch + SBC_MAX] = dest_states_word[j];
  3097. }
  3098. }
  3099. /* new line */
  3100. if (bitset_contain (acceptable, NEWLINE_CHAR))
  3101. {
  3102. /* The current state accepts newline character. */
  3103. for (j = 0; j < ndests; ++j)
  3104. if (bitset_contain (dests_ch[j], NEWLINE_CHAR))
  3105. {
  3106. /* k-th destination accepts newline character. */
  3107. trtable[NEWLINE_CHAR] = dest_states_nl[j];
  3108. if (need_word_trtable)
  3109. trtable[NEWLINE_CHAR + SBC_MAX] = dest_states_nl[j];
  3110. /* There must be only one destination which accepts
  3111. newline. See group_nodes_into_DFAstates. */
  3112. break;
  3113. }
  3114. }
  3115. re_node_set_free (&follows);
  3116. for (i = 0; i < ndests; ++i)
  3117. re_node_set_free (dests_node + i);
  3118. return true;
  3119. }
  3120. /* Group all nodes belonging to STATE into several destinations.
  3121. Then for all destinations, set the nodes belonging to the destination
  3122. to DESTS_NODE[i] and set the characters accepted by the destination
  3123. to DEST_CH[i]. This function return the number of destinations. */
  3124. static Idx
  3125. group_nodes_into_DFAstates (const re_dfa_t *dfa, const re_dfastate_t *state,
  3126. re_node_set *dests_node, bitset_t *dests_ch)
  3127. {
  3128. reg_errcode_t err;
  3129. bool ok;
  3130. Idx i, j, k;
  3131. Idx ndests; /* Number of the destinations from 'state'. */
  3132. bitset_t accepts; /* Characters a node can accept. */
  3133. const re_node_set *cur_nodes = &state->nodes;
  3134. bitset_empty (accepts);
  3135. ndests = 0;
  3136. /* For all the nodes belonging to 'state', */
  3137. for (i = 0; i < cur_nodes->nelem; ++i)
  3138. {
  3139. re_token_t *node = &dfa->nodes[cur_nodes->elems[i]];
  3140. re_token_type_t type = node->type;
  3141. unsigned int constraint = node->constraint;
  3142. /* Enumerate all single byte character this node can accept. */
  3143. if (type == CHARACTER)
  3144. bitset_set (accepts, node->opr.c);
  3145. else if (type == SIMPLE_BRACKET)
  3146. {
  3147. bitset_merge (accepts, node->opr.sbcset);
  3148. }
  3149. else if (type == OP_PERIOD)
  3150. {
  3151. #ifdef RE_ENABLE_I18N
  3152. if (dfa->mb_cur_max > 1)
  3153. bitset_merge (accepts, dfa->sb_char);
  3154. else
  3155. #endif
  3156. bitset_set_all (accepts);
  3157. if (!(dfa->syntax & RE_DOT_NEWLINE))
  3158. bitset_clear (accepts, '\n');
  3159. if (dfa->syntax & RE_DOT_NOT_NULL)
  3160. bitset_clear (accepts, '\0');
  3161. }
  3162. #ifdef RE_ENABLE_I18N
  3163. else if (type == OP_UTF8_PERIOD)
  3164. {
  3165. if (ASCII_CHARS % BITSET_WORD_BITS == 0)
  3166. memset (accepts, -1, ASCII_CHARS / CHAR_BIT);
  3167. else
  3168. bitset_merge (accepts, utf8_sb_map);
  3169. if (!(dfa->syntax & RE_DOT_NEWLINE))
  3170. bitset_clear (accepts, '\n');
  3171. if (dfa->syntax & RE_DOT_NOT_NULL)
  3172. bitset_clear (accepts, '\0');
  3173. }
  3174. #endif
  3175. else
  3176. continue;
  3177. /* Check the 'accepts' and sift the characters which are not
  3178. match it the context. */
  3179. if (constraint)
  3180. {
  3181. if (constraint & NEXT_NEWLINE_CONSTRAINT)
  3182. {
  3183. bool accepts_newline = bitset_contain (accepts, NEWLINE_CHAR);
  3184. bitset_empty (accepts);
  3185. if (accepts_newline)
  3186. bitset_set (accepts, NEWLINE_CHAR);
  3187. else
  3188. continue;
  3189. }
  3190. if (constraint & NEXT_ENDBUF_CONSTRAINT)
  3191. {
  3192. bitset_empty (accepts);
  3193. continue;
  3194. }
  3195. if (constraint & NEXT_WORD_CONSTRAINT)
  3196. {
  3197. bitset_word_t any_set = 0;
  3198. if (type == CHARACTER && !node->word_char)
  3199. {
  3200. bitset_empty (accepts);
  3201. continue;
  3202. }
  3203. #ifdef RE_ENABLE_I18N
  3204. if (dfa->mb_cur_max > 1)
  3205. for (j = 0; j < BITSET_WORDS; ++j)
  3206. any_set |= (accepts[j] &= (dfa->word_char[j] | ~dfa->sb_char[j]));
  3207. else
  3208. #endif
  3209. for (j = 0; j < BITSET_WORDS; ++j)
  3210. any_set |= (accepts[j] &= dfa->word_char[j]);
  3211. if (!any_set)
  3212. continue;
  3213. }
  3214. if (constraint & NEXT_NOTWORD_CONSTRAINT)
  3215. {
  3216. bitset_word_t any_set = 0;
  3217. if (type == CHARACTER && node->word_char)
  3218. {
  3219. bitset_empty (accepts);
  3220. continue;
  3221. }
  3222. #ifdef RE_ENABLE_I18N
  3223. if (dfa->mb_cur_max > 1)
  3224. for (j = 0; j < BITSET_WORDS; ++j)
  3225. any_set |= (accepts[j] &= ~(dfa->word_char[j] & dfa->sb_char[j]));
  3226. else
  3227. #endif
  3228. for (j = 0; j < BITSET_WORDS; ++j)
  3229. any_set |= (accepts[j] &= ~dfa->word_char[j]);
  3230. if (!any_set)
  3231. continue;
  3232. }
  3233. }
  3234. /* Then divide 'accepts' into DFA states, or create a new
  3235. state. Above, we make sure that accepts is not empty. */
  3236. for (j = 0; j < ndests; ++j)
  3237. {
  3238. bitset_t intersec; /* Intersection sets, see below. */
  3239. bitset_t remains;
  3240. /* Flags, see below. */
  3241. bitset_word_t has_intersec, not_subset, not_consumed;
  3242. /* Optimization, skip if this state doesn't accept the character. */
  3243. if (type == CHARACTER && !bitset_contain (dests_ch[j], node->opr.c))
  3244. continue;
  3245. /* Enumerate the intersection set of this state and 'accepts'. */
  3246. has_intersec = 0;
  3247. for (k = 0; k < BITSET_WORDS; ++k)
  3248. has_intersec |= intersec[k] = accepts[k] & dests_ch[j][k];
  3249. /* And skip if the intersection set is empty. */
  3250. if (!has_intersec)
  3251. continue;
  3252. /* Then check if this state is a subset of 'accepts'. */
  3253. not_subset = not_consumed = 0;
  3254. for (k = 0; k < BITSET_WORDS; ++k)
  3255. {
  3256. not_subset |= remains[k] = ~accepts[k] & dests_ch[j][k];
  3257. not_consumed |= accepts[k] = accepts[k] & ~dests_ch[j][k];
  3258. }
  3259. /* If this state isn't a subset of 'accepts', create a
  3260. new group state, which has the 'remains'. */
  3261. if (not_subset)
  3262. {
  3263. bitset_copy (dests_ch[ndests], remains);
  3264. bitset_copy (dests_ch[j], intersec);
  3265. err = re_node_set_init_copy (dests_node + ndests, &dests_node[j]);
  3266. if (__glibc_unlikely (err != REG_NOERROR))
  3267. goto error_return;
  3268. ++ndests;
  3269. }
  3270. /* Put the position in the current group. */
  3271. ok = re_node_set_insert (&dests_node[j], cur_nodes->elems[i]);
  3272. if (__glibc_unlikely (! ok))
  3273. goto error_return;
  3274. /* If all characters are consumed, go to next node. */
  3275. if (!not_consumed)
  3276. break;
  3277. }
  3278. /* Some characters remain, create a new group. */
  3279. if (j == ndests)
  3280. {
  3281. bitset_copy (dests_ch[ndests], accepts);
  3282. err = re_node_set_init_1 (dests_node + ndests, cur_nodes->elems[i]);
  3283. if (__glibc_unlikely (err != REG_NOERROR))
  3284. goto error_return;
  3285. ++ndests;
  3286. bitset_empty (accepts);
  3287. }
  3288. }
  3289. assume (ndests <= SBC_MAX);
  3290. return ndests;
  3291. error_return:
  3292. for (j = 0; j < ndests; ++j)
  3293. re_node_set_free (dests_node + j);
  3294. return -1;
  3295. }
  3296. #ifdef RE_ENABLE_I18N
  3297. /* Check how many bytes the node 'dfa->nodes[node_idx]' accepts.
  3298. Return the number of the bytes the node accepts.
  3299. STR_IDX is the current index of the input string.
  3300. This function handles the nodes which can accept one character, or
  3301. one collating element like '.', '[a-z]', opposite to the other nodes
  3302. can only accept one byte. */
  3303. # ifdef _LIBC
  3304. # include <locale/weight.h>
  3305. # endif
  3306. static int
  3307. check_node_accept_bytes (const re_dfa_t *dfa, Idx node_idx,
  3308. const re_string_t *input, Idx str_idx)
  3309. {
  3310. const re_token_t *node = dfa->nodes + node_idx;
  3311. int char_len, elem_len;
  3312. Idx i;
  3313. if (__glibc_unlikely (node->type == OP_UTF8_PERIOD))
  3314. {
  3315. unsigned char c = re_string_byte_at (input, str_idx), d;
  3316. if (__glibc_likely (c < 0xc2))
  3317. return 0;
  3318. if (str_idx + 2 > input->len)
  3319. return 0;
  3320. d = re_string_byte_at (input, str_idx + 1);
  3321. if (c < 0xe0)
  3322. return (d < 0x80 || d > 0xbf) ? 0 : 2;
  3323. else if (c < 0xf0)
  3324. {
  3325. char_len = 3;
  3326. if (c == 0xe0 && d < 0xa0)
  3327. return 0;
  3328. }
  3329. else if (c < 0xf8)
  3330. {
  3331. char_len = 4;
  3332. if (c == 0xf0 && d < 0x90)
  3333. return 0;
  3334. }
  3335. else if (c < 0xfc)
  3336. {
  3337. char_len = 5;
  3338. if (c == 0xf8 && d < 0x88)
  3339. return 0;
  3340. }
  3341. else if (c < 0xfe)
  3342. {
  3343. char_len = 6;
  3344. if (c == 0xfc && d < 0x84)
  3345. return 0;
  3346. }
  3347. else
  3348. return 0;
  3349. if (str_idx + char_len > input->len)
  3350. return 0;
  3351. for (i = 1; i < char_len; ++i)
  3352. {
  3353. d = re_string_byte_at (input, str_idx + i);
  3354. if (d < 0x80 || d > 0xbf)
  3355. return 0;
  3356. }
  3357. return char_len;
  3358. }
  3359. char_len = re_string_char_size_at (input, str_idx);
  3360. if (node->type == OP_PERIOD)
  3361. {
  3362. if (char_len <= 1)
  3363. return 0;
  3364. /* FIXME: I don't think this if is needed, as both '\n'
  3365. and '\0' are char_len == 1. */
  3366. /* '.' accepts any one character except the following two cases. */
  3367. if ((!(dfa->syntax & RE_DOT_NEWLINE)
  3368. && re_string_byte_at (input, str_idx) == '\n')
  3369. || ((dfa->syntax & RE_DOT_NOT_NULL)
  3370. && re_string_byte_at (input, str_idx) == '\0'))
  3371. return 0;
  3372. return char_len;
  3373. }
  3374. elem_len = re_string_elem_size_at (input, str_idx);
  3375. if ((elem_len <= 1 && char_len <= 1) || char_len == 0)
  3376. return 0;
  3377. if (node->type == COMPLEX_BRACKET)
  3378. {
  3379. const re_charset_t *cset = node->opr.mbcset;
  3380. # ifdef _LIBC
  3381. const unsigned char *pin
  3382. = ((const unsigned char *) re_string_get_buffer (input) + str_idx);
  3383. Idx j;
  3384. uint32_t nrules;
  3385. # endif /* _LIBC */
  3386. int match_len = 0;
  3387. wchar_t wc = ((cset->nranges || cset->nchar_classes || cset->nmbchars)
  3388. ? re_string_wchar_at (input, str_idx) : 0);
  3389. /* match with multibyte character? */
  3390. for (i = 0; i < cset->nmbchars; ++i)
  3391. if (wc == cset->mbchars[i])
  3392. {
  3393. match_len = char_len;
  3394. goto check_node_accept_bytes_match;
  3395. }
  3396. /* match with character_class? */
  3397. for (i = 0; i < cset->nchar_classes; ++i)
  3398. {
  3399. wctype_t wt = cset->char_classes[i];
  3400. if (__iswctype (wc, wt))
  3401. {
  3402. match_len = char_len;
  3403. goto check_node_accept_bytes_match;
  3404. }
  3405. }
  3406. # ifdef _LIBC
  3407. nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
  3408. if (nrules != 0)
  3409. {
  3410. unsigned int in_collseq = 0;
  3411. const int32_t *table, *indirect;
  3412. const unsigned char *weights, *extra;
  3413. const char *collseqwc;
  3414. /* match with collating_symbol? */
  3415. if (cset->ncoll_syms)
  3416. extra = (const unsigned char *)
  3417. _NL_CURRENT (LC_COLLATE, _NL_COLLATE_SYMB_EXTRAMB);
  3418. for (i = 0; i < cset->ncoll_syms; ++i)
  3419. {
  3420. const unsigned char *coll_sym = extra + cset->coll_syms[i];
  3421. /* Compare the length of input collating element and
  3422. the length of current collating element. */
  3423. if (*coll_sym != elem_len)
  3424. continue;
  3425. /* Compare each bytes. */
  3426. for (j = 0; j < *coll_sym; j++)
  3427. if (pin[j] != coll_sym[1 + j])
  3428. break;
  3429. if (j == *coll_sym)
  3430. {
  3431. /* Match if every bytes is equal. */
  3432. match_len = j;
  3433. goto check_node_accept_bytes_match;
  3434. }
  3435. }
  3436. if (cset->nranges)
  3437. {
  3438. if (elem_len <= char_len)
  3439. {
  3440. collseqwc = _NL_CURRENT (LC_COLLATE, _NL_COLLATE_COLLSEQWC);
  3441. in_collseq = __collseq_table_lookup (collseqwc, wc);
  3442. }
  3443. else
  3444. in_collseq = find_collation_sequence_value (pin, elem_len);
  3445. }
  3446. /* match with range expression? */
  3447. /* FIXME: Implement rational ranges here, too. */
  3448. for (i = 0; i < cset->nranges; ++i)
  3449. if (cset->range_starts[i] <= in_collseq
  3450. && in_collseq <= cset->range_ends[i])
  3451. {
  3452. match_len = elem_len;
  3453. goto check_node_accept_bytes_match;
  3454. }
  3455. /* match with equivalence_class? */
  3456. if (cset->nequiv_classes)
  3457. {
  3458. const unsigned char *cp = pin;
  3459. table = (const int32_t *)
  3460. _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB);
  3461. weights = (const unsigned char *)
  3462. _NL_CURRENT (LC_COLLATE, _NL_COLLATE_WEIGHTMB);
  3463. extra = (const unsigned char *)
  3464. _NL_CURRENT (LC_COLLATE, _NL_COLLATE_EXTRAMB);
  3465. indirect = (const int32_t *)
  3466. _NL_CURRENT (LC_COLLATE, _NL_COLLATE_INDIRECTMB);
  3467. int32_t idx = findidx (table, indirect, extra, &cp, elem_len);
  3468. int32_t rule = idx >> 24;
  3469. idx &= 0xffffff;
  3470. if (idx > 0)
  3471. {
  3472. size_t weight_len = weights[idx];
  3473. for (i = 0; i < cset->nequiv_classes; ++i)
  3474. {
  3475. int32_t equiv_class_idx = cset->equiv_classes[i];
  3476. int32_t equiv_class_rule = equiv_class_idx >> 24;
  3477. equiv_class_idx &= 0xffffff;
  3478. if (weights[equiv_class_idx] == weight_len
  3479. && equiv_class_rule == rule
  3480. && memcmp (weights + idx + 1,
  3481. weights + equiv_class_idx + 1,
  3482. weight_len) == 0)
  3483. {
  3484. match_len = elem_len;
  3485. goto check_node_accept_bytes_match;
  3486. }
  3487. }
  3488. }
  3489. }
  3490. }
  3491. else
  3492. # endif /* _LIBC */
  3493. {
  3494. /* match with range expression? */
  3495. for (i = 0; i < cset->nranges; ++i)
  3496. {
  3497. if (cset->range_starts[i] <= wc && wc <= cset->range_ends[i])
  3498. {
  3499. match_len = char_len;
  3500. goto check_node_accept_bytes_match;
  3501. }
  3502. }
  3503. }
  3504. check_node_accept_bytes_match:
  3505. if (!cset->non_match)
  3506. return match_len;
  3507. else
  3508. {
  3509. if (match_len > 0)
  3510. return 0;
  3511. else
  3512. return (elem_len > char_len) ? elem_len : char_len;
  3513. }
  3514. }
  3515. return 0;
  3516. }
  3517. # ifdef _LIBC
  3518. static unsigned int
  3519. find_collation_sequence_value (const unsigned char *mbs, size_t mbs_len)
  3520. {
  3521. uint32_t nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
  3522. if (nrules == 0)
  3523. {
  3524. if (mbs_len == 1)
  3525. {
  3526. /* No valid character. Match it as a single byte character. */
  3527. const unsigned char *collseq = (const unsigned char *)
  3528. _NL_CURRENT (LC_COLLATE, _NL_COLLATE_COLLSEQMB);
  3529. return collseq[mbs[0]];
  3530. }
  3531. return UINT_MAX;
  3532. }
  3533. else
  3534. {
  3535. int32_t idx;
  3536. const unsigned char *extra = (const unsigned char *)
  3537. _NL_CURRENT (LC_COLLATE, _NL_COLLATE_SYMB_EXTRAMB);
  3538. int32_t extrasize = (const unsigned char *)
  3539. _NL_CURRENT (LC_COLLATE, _NL_COLLATE_SYMB_EXTRAMB + 1) - extra;
  3540. for (idx = 0; idx < extrasize;)
  3541. {
  3542. int mbs_cnt;
  3543. bool found = false;
  3544. int32_t elem_mbs_len;
  3545. /* Skip the name of collating element name. */
  3546. idx = idx + extra[idx] + 1;
  3547. elem_mbs_len = extra[idx++];
  3548. if (mbs_len == elem_mbs_len)
  3549. {
  3550. for (mbs_cnt = 0; mbs_cnt < elem_mbs_len; ++mbs_cnt)
  3551. if (extra[idx + mbs_cnt] != mbs[mbs_cnt])
  3552. break;
  3553. if (mbs_cnt == elem_mbs_len)
  3554. /* Found the entry. */
  3555. found = true;
  3556. }
  3557. /* Skip the byte sequence of the collating element. */
  3558. idx += elem_mbs_len;
  3559. /* Adjust for the alignment. */
  3560. idx = (idx + 3) & ~3;
  3561. /* Skip the collation sequence value. */
  3562. idx += sizeof (uint32_t);
  3563. /* Skip the wide char sequence of the collating element. */
  3564. idx = idx + sizeof (uint32_t) * (*(int32_t *) (extra + idx) + 1);
  3565. /* If we found the entry, return the sequence value. */
  3566. if (found)
  3567. return *(uint32_t *) (extra + idx);
  3568. /* Skip the collation sequence value. */
  3569. idx += sizeof (uint32_t);
  3570. }
  3571. return UINT_MAX;
  3572. }
  3573. }
  3574. # endif /* _LIBC */
  3575. #endif /* RE_ENABLE_I18N */
  3576. /* Check whether the node accepts the byte which is IDX-th
  3577. byte of the INPUT. */
  3578. static bool
  3579. check_node_accept (const re_match_context_t *mctx, const re_token_t *node,
  3580. Idx idx)
  3581. {
  3582. unsigned char ch;
  3583. ch = re_string_byte_at (&mctx->input, idx);
  3584. switch (node->type)
  3585. {
  3586. case CHARACTER:
  3587. if (node->opr.c != ch)
  3588. return false;
  3589. break;
  3590. case SIMPLE_BRACKET:
  3591. if (!bitset_contain (node->opr.sbcset, ch))
  3592. return false;
  3593. break;
  3594. #ifdef RE_ENABLE_I18N
  3595. case OP_UTF8_PERIOD:
  3596. if (ch >= ASCII_CHARS)
  3597. return false;
  3598. FALLTHROUGH;
  3599. #endif
  3600. case OP_PERIOD:
  3601. if ((ch == '\n' && !(mctx->dfa->syntax & RE_DOT_NEWLINE))
  3602. || (ch == '\0' && (mctx->dfa->syntax & RE_DOT_NOT_NULL)))
  3603. return false;
  3604. break;
  3605. default:
  3606. return false;
  3607. }
  3608. if (node->constraint)
  3609. {
  3610. /* The node has constraints. Check whether the current context
  3611. satisfies the constraints. */
  3612. unsigned int context = re_string_context_at (&mctx->input, idx,
  3613. mctx->eflags);
  3614. if (NOT_SATISFY_NEXT_CONSTRAINT (node->constraint, context))
  3615. return false;
  3616. }
  3617. return true;
  3618. }
  3619. /* Extend the buffers, if the buffers have run out. */
  3620. static reg_errcode_t
  3621. __attribute_warn_unused_result__
  3622. extend_buffers (re_match_context_t *mctx, int min_len)
  3623. {
  3624. reg_errcode_t ret;
  3625. re_string_t *pstr = &mctx->input;
  3626. /* Avoid overflow. */
  3627. if (__glibc_unlikely (MIN (IDX_MAX, SIZE_MAX / sizeof (re_dfastate_t *)) / 2
  3628. <= pstr->bufs_len))
  3629. return REG_ESPACE;
  3630. /* Double the lengths of the buffers, but allocate at least MIN_LEN. */
  3631. ret = re_string_realloc_buffers (pstr,
  3632. MAX (min_len,
  3633. MIN (pstr->len, pstr->bufs_len * 2)));
  3634. if (__glibc_unlikely (ret != REG_NOERROR))
  3635. return ret;
  3636. if (mctx->state_log != NULL)
  3637. {
  3638. /* And double the length of state_log. */
  3639. /* XXX We have no indication of the size of this buffer. If this
  3640. allocation fail we have no indication that the state_log array
  3641. does not have the right size. */
  3642. re_dfastate_t **new_array = re_realloc (mctx->state_log, re_dfastate_t *,
  3643. pstr->bufs_len + 1);
  3644. if (__glibc_unlikely (new_array == NULL))
  3645. return REG_ESPACE;
  3646. mctx->state_log = new_array;
  3647. }
  3648. /* Then reconstruct the buffers. */
  3649. if (pstr->icase)
  3650. {
  3651. #ifdef RE_ENABLE_I18N
  3652. if (pstr->mb_cur_max > 1)
  3653. {
  3654. ret = build_wcs_upper_buffer (pstr);
  3655. if (__glibc_unlikely (ret != REG_NOERROR))
  3656. return ret;
  3657. }
  3658. else
  3659. #endif /* RE_ENABLE_I18N */
  3660. build_upper_buffer (pstr);
  3661. }
  3662. else
  3663. {
  3664. #ifdef RE_ENABLE_I18N
  3665. if (pstr->mb_cur_max > 1)
  3666. build_wcs_buffer (pstr);
  3667. else
  3668. #endif /* RE_ENABLE_I18N */
  3669. {
  3670. if (pstr->trans != NULL)
  3671. re_string_translate_buffer (pstr);
  3672. }
  3673. }
  3674. return REG_NOERROR;
  3675. }
  3676. /* Functions for matching context. */
  3677. /* Initialize MCTX. */
  3678. static reg_errcode_t
  3679. __attribute_warn_unused_result__
  3680. match_ctx_init (re_match_context_t *mctx, int eflags, Idx n)
  3681. {
  3682. mctx->eflags = eflags;
  3683. mctx->match_last = -1;
  3684. if (n > 0)
  3685. {
  3686. /* Avoid overflow. */
  3687. size_t max_object_size =
  3688. MAX (sizeof (struct re_backref_cache_entry),
  3689. sizeof (re_sub_match_top_t *));
  3690. if (__glibc_unlikely (MIN (IDX_MAX, SIZE_MAX / max_object_size) < n))
  3691. return REG_ESPACE;
  3692. mctx->bkref_ents = re_malloc (struct re_backref_cache_entry, n);
  3693. mctx->sub_tops = re_malloc (re_sub_match_top_t *, n);
  3694. if (__glibc_unlikely (mctx->bkref_ents == NULL || mctx->sub_tops == NULL))
  3695. return REG_ESPACE;
  3696. }
  3697. /* Already zero-ed by the caller.
  3698. else
  3699. mctx->bkref_ents = NULL;
  3700. mctx->nbkref_ents = 0;
  3701. mctx->nsub_tops = 0; */
  3702. mctx->abkref_ents = n;
  3703. mctx->max_mb_elem_len = 1;
  3704. mctx->asub_tops = n;
  3705. return REG_NOERROR;
  3706. }
  3707. /* Clean the entries which depend on the current input in MCTX.
  3708. This function must be invoked when the matcher changes the start index
  3709. of the input, or changes the input string. */
  3710. static void
  3711. match_ctx_clean (re_match_context_t *mctx)
  3712. {
  3713. Idx st_idx;
  3714. for (st_idx = 0; st_idx < mctx->nsub_tops; ++st_idx)
  3715. {
  3716. Idx sl_idx;
  3717. re_sub_match_top_t *top = mctx->sub_tops[st_idx];
  3718. for (sl_idx = 0; sl_idx < top->nlasts; ++sl_idx)
  3719. {
  3720. re_sub_match_last_t *last = top->lasts[sl_idx];
  3721. re_free (last->path.array);
  3722. re_free (last);
  3723. }
  3724. re_free (top->lasts);
  3725. if (top->path)
  3726. {
  3727. re_free (top->path->array);
  3728. re_free (top->path);
  3729. }
  3730. re_free (top);
  3731. }
  3732. mctx->nsub_tops = 0;
  3733. mctx->nbkref_ents = 0;
  3734. }
  3735. /* Free all the memory associated with MCTX. */
  3736. static void
  3737. match_ctx_free (re_match_context_t *mctx)
  3738. {
  3739. /* First, free all the memory associated with MCTX->SUB_TOPS. */
  3740. match_ctx_clean (mctx);
  3741. re_free (mctx->sub_tops);
  3742. re_free (mctx->bkref_ents);
  3743. }
  3744. /* Add a new backreference entry to MCTX.
  3745. Note that we assume that caller never call this function with duplicate
  3746. entry, and call with STR_IDX which isn't smaller than any existing entry.
  3747. */
  3748. static reg_errcode_t
  3749. __attribute_warn_unused_result__
  3750. match_ctx_add_entry (re_match_context_t *mctx, Idx node, Idx str_idx, Idx from,
  3751. Idx to)
  3752. {
  3753. if (mctx->nbkref_ents >= mctx->abkref_ents)
  3754. {
  3755. struct re_backref_cache_entry* new_entry;
  3756. new_entry = re_realloc (mctx->bkref_ents, struct re_backref_cache_entry,
  3757. mctx->abkref_ents * 2);
  3758. if (__glibc_unlikely (new_entry == NULL))
  3759. {
  3760. re_free (mctx->bkref_ents);
  3761. return REG_ESPACE;
  3762. }
  3763. mctx->bkref_ents = new_entry;
  3764. memset (mctx->bkref_ents + mctx->nbkref_ents, '\0',
  3765. sizeof (struct re_backref_cache_entry) * mctx->abkref_ents);
  3766. mctx->abkref_ents *= 2;
  3767. }
  3768. if (mctx->nbkref_ents > 0
  3769. && mctx->bkref_ents[mctx->nbkref_ents - 1].str_idx == str_idx)
  3770. mctx->bkref_ents[mctx->nbkref_ents - 1].more = 1;
  3771. mctx->bkref_ents[mctx->nbkref_ents].node = node;
  3772. mctx->bkref_ents[mctx->nbkref_ents].str_idx = str_idx;
  3773. mctx->bkref_ents[mctx->nbkref_ents].subexp_from = from;
  3774. mctx->bkref_ents[mctx->nbkref_ents].subexp_to = to;
  3775. /* This is a cache that saves negative results of check_dst_limits_calc_pos.
  3776. If bit N is clear, means that this entry won't epsilon-transition to
  3777. an OP_OPEN_SUBEXP or OP_CLOSE_SUBEXP for the N+1-th subexpression. If
  3778. it is set, check_dst_limits_calc_pos_1 will recurse and try to find one
  3779. such node.
  3780. A backreference does not epsilon-transition unless it is empty, so set
  3781. to all zeros if FROM != TO. */
  3782. mctx->bkref_ents[mctx->nbkref_ents].eps_reachable_subexps_map
  3783. = (from == to ? -1 : 0);
  3784. mctx->bkref_ents[mctx->nbkref_ents++].more = 0;
  3785. if (mctx->max_mb_elem_len < to - from)
  3786. mctx->max_mb_elem_len = to - from;
  3787. return REG_NOERROR;
  3788. }
  3789. /* Return the first entry with the same str_idx, or -1 if none is
  3790. found. Note that MCTX->BKREF_ENTS is already sorted by MCTX->STR_IDX. */
  3791. static Idx
  3792. search_cur_bkref_entry (const re_match_context_t *mctx, Idx str_idx)
  3793. {
  3794. Idx left, right, mid, last;
  3795. last = right = mctx->nbkref_ents;
  3796. for (left = 0; left < right;)
  3797. {
  3798. mid = (left + right) / 2;
  3799. if (mctx->bkref_ents[mid].str_idx < str_idx)
  3800. left = mid + 1;
  3801. else
  3802. right = mid;
  3803. }
  3804. if (left < last && mctx->bkref_ents[left].str_idx == str_idx)
  3805. return left;
  3806. else
  3807. return -1;
  3808. }
  3809. /* Register the node NODE, whose type is OP_OPEN_SUBEXP, and which matches
  3810. at STR_IDX. */
  3811. static reg_errcode_t
  3812. __attribute_warn_unused_result__
  3813. match_ctx_add_subtop (re_match_context_t *mctx, Idx node, Idx str_idx)
  3814. {
  3815. DEBUG_ASSERT (mctx->sub_tops != NULL);
  3816. DEBUG_ASSERT (mctx->asub_tops > 0);
  3817. if (__glibc_unlikely (mctx->nsub_tops == mctx->asub_tops))
  3818. {
  3819. Idx new_asub_tops = mctx->asub_tops * 2;
  3820. re_sub_match_top_t **new_array = re_realloc (mctx->sub_tops,
  3821. re_sub_match_top_t *,
  3822. new_asub_tops);
  3823. if (__glibc_unlikely (new_array == NULL))
  3824. return REG_ESPACE;
  3825. mctx->sub_tops = new_array;
  3826. mctx->asub_tops = new_asub_tops;
  3827. }
  3828. mctx->sub_tops[mctx->nsub_tops] = calloc (1, sizeof (re_sub_match_top_t));
  3829. if (__glibc_unlikely (mctx->sub_tops[mctx->nsub_tops] == NULL))
  3830. return REG_ESPACE;
  3831. mctx->sub_tops[mctx->nsub_tops]->node = node;
  3832. mctx->sub_tops[mctx->nsub_tops++]->str_idx = str_idx;
  3833. return REG_NOERROR;
  3834. }
  3835. /* Register the node NODE, whose type is OP_CLOSE_SUBEXP, and which matches
  3836. at STR_IDX, whose corresponding OP_OPEN_SUBEXP is SUB_TOP. */
  3837. static re_sub_match_last_t *
  3838. match_ctx_add_sublast (re_sub_match_top_t *subtop, Idx node, Idx str_idx)
  3839. {
  3840. re_sub_match_last_t *new_entry;
  3841. if (__glibc_unlikely (subtop->nlasts == subtop->alasts))
  3842. {
  3843. Idx new_alasts = 2 * subtop->alasts + 1;
  3844. re_sub_match_last_t **new_array = re_realloc (subtop->lasts,
  3845. re_sub_match_last_t *,
  3846. new_alasts);
  3847. if (__glibc_unlikely (new_array == NULL))
  3848. return NULL;
  3849. subtop->lasts = new_array;
  3850. subtop->alasts = new_alasts;
  3851. }
  3852. new_entry = calloc (1, sizeof (re_sub_match_last_t));
  3853. if (__glibc_likely (new_entry != NULL))
  3854. {
  3855. subtop->lasts[subtop->nlasts] = new_entry;
  3856. new_entry->node = node;
  3857. new_entry->str_idx = str_idx;
  3858. ++subtop->nlasts;
  3859. }
  3860. return new_entry;
  3861. }
  3862. static void
  3863. sift_ctx_init (re_sift_context_t *sctx, re_dfastate_t **sifted_sts,
  3864. re_dfastate_t **limited_sts, Idx last_node, Idx last_str_idx)
  3865. {
  3866. sctx->sifted_states = sifted_sts;
  3867. sctx->limited_states = limited_sts;
  3868. sctx->last_node = last_node;
  3869. sctx->last_str_idx = last_str_idx;
  3870. re_node_set_init_empty (&sctx->limits);
  3871. }