regexec.c 127 KB

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