regexec.c 123 KB

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