regexec.c 126 KB

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