regexec.c 124 KB

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