regexec.c 123 KB

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