regexec.c 128 KB

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