regexec.c 124 KB

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