regexec.c 128 KB

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