regexec.c 128 KB

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