regexec.c 126 KB

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