xmlregexp.c 218 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272
  1. /*
  2. * regexp.c: generic and extensible Regular Expression engine
  3. *
  4. * Basically designed with the purpose of compiling regexps for
  5. * the variety of validation/schemas mechanisms now available in
  6. * XML related specifications these include:
  7. * - XML-1.0 DTD validation
  8. * - XML Schemas structure part 1
  9. * - XML Schemas Datatypes part 2 especially Appendix F
  10. * - RELAX-NG/TREX i.e. the counter proposal
  11. *
  12. * See Copyright for the status of this software.
  13. *
  14. * Daniel Veillard <veillard@redhat.com>
  15. */
  16. #define IN_LIBXML
  17. #include "libxml.h"
  18. #ifdef LIBXML_REGEXP_ENABLED
  19. /* #define DEBUG_ERR */
  20. #include <stdio.h>
  21. #include <string.h>
  22. #ifdef HAVE_LIMITS_H
  23. #include <limits.h>
  24. #endif
  25. #ifdef HAVE_STDINT_H
  26. #include <stdint.h>
  27. #endif
  28. #include <libxml/tree.h>
  29. #include <libxml/parserInternals.h>
  30. #include <libxml/xmlregexp.h>
  31. #include <libxml/xmlautomata.h>
  32. #include <libxml/xmlunicode.h>
  33. #ifndef INT_MAX
  34. #define INT_MAX 123456789 /* easy to flag and big enough for our needs */
  35. #endif
  36. #ifndef SIZE_MAX
  37. #define SIZE_MAX ((size_t) -1)
  38. #endif
  39. /* #define DEBUG_REGEXP_GRAPH */
  40. /* #define DEBUG_REGEXP_EXEC */
  41. /* #define DEBUG_PUSH */
  42. /* #define DEBUG_COMPACTION */
  43. #define MAX_PUSH 10000000
  44. #ifdef ERROR
  45. #undef ERROR
  46. #endif
  47. #define ERROR(str) \
  48. ctxt->error = XML_REGEXP_COMPILE_ERROR; \
  49. xmlRegexpErrCompile(ctxt, str);
  50. #define NEXT ctxt->cur++
  51. #define CUR (*(ctxt->cur))
  52. #define NXT(index) (ctxt->cur[index])
  53. #define CUR_SCHAR(s, l) xmlStringCurrentChar(NULL, s, &l)
  54. #define NEXTL(l) ctxt->cur += l;
  55. #define XML_REG_STRING_SEPARATOR '|'
  56. /*
  57. * Need PREV to check on a '-' within a Character Group. May only be used
  58. * when it's guaranteed that cur is not at the beginning of ctxt->string!
  59. */
  60. #define PREV (ctxt->cur[-1])
  61. /**
  62. * TODO:
  63. *
  64. * macro to flag unimplemented blocks
  65. */
  66. #define TODO \
  67. xmlGenericError(xmlGenericErrorContext, \
  68. "Unimplemented block at %s:%d\n", \
  69. __FILE__, __LINE__);
  70. /************************************************************************
  71. * *
  72. * Datatypes and structures *
  73. * *
  74. ************************************************************************/
  75. /*
  76. * Note: the order of the enums below is significant, do not shuffle
  77. */
  78. typedef enum {
  79. XML_REGEXP_EPSILON = 1,
  80. XML_REGEXP_CHARVAL,
  81. XML_REGEXP_RANGES,
  82. XML_REGEXP_SUBREG, /* used for () sub regexps */
  83. XML_REGEXP_STRING,
  84. XML_REGEXP_ANYCHAR, /* . */
  85. XML_REGEXP_ANYSPACE, /* \s */
  86. XML_REGEXP_NOTSPACE, /* \S */
  87. XML_REGEXP_INITNAME, /* \l */
  88. XML_REGEXP_NOTINITNAME, /* \L */
  89. XML_REGEXP_NAMECHAR, /* \c */
  90. XML_REGEXP_NOTNAMECHAR, /* \C */
  91. XML_REGEXP_DECIMAL, /* \d */
  92. XML_REGEXP_NOTDECIMAL, /* \D */
  93. XML_REGEXP_REALCHAR, /* \w */
  94. XML_REGEXP_NOTREALCHAR, /* \W */
  95. XML_REGEXP_LETTER = 100,
  96. XML_REGEXP_LETTER_UPPERCASE,
  97. XML_REGEXP_LETTER_LOWERCASE,
  98. XML_REGEXP_LETTER_TITLECASE,
  99. XML_REGEXP_LETTER_MODIFIER,
  100. XML_REGEXP_LETTER_OTHERS,
  101. XML_REGEXP_MARK,
  102. XML_REGEXP_MARK_NONSPACING,
  103. XML_REGEXP_MARK_SPACECOMBINING,
  104. XML_REGEXP_MARK_ENCLOSING,
  105. XML_REGEXP_NUMBER,
  106. XML_REGEXP_NUMBER_DECIMAL,
  107. XML_REGEXP_NUMBER_LETTER,
  108. XML_REGEXP_NUMBER_OTHERS,
  109. XML_REGEXP_PUNCT,
  110. XML_REGEXP_PUNCT_CONNECTOR,
  111. XML_REGEXP_PUNCT_DASH,
  112. XML_REGEXP_PUNCT_OPEN,
  113. XML_REGEXP_PUNCT_CLOSE,
  114. XML_REGEXP_PUNCT_INITQUOTE,
  115. XML_REGEXP_PUNCT_FINQUOTE,
  116. XML_REGEXP_PUNCT_OTHERS,
  117. XML_REGEXP_SEPAR,
  118. XML_REGEXP_SEPAR_SPACE,
  119. XML_REGEXP_SEPAR_LINE,
  120. XML_REGEXP_SEPAR_PARA,
  121. XML_REGEXP_SYMBOL,
  122. XML_REGEXP_SYMBOL_MATH,
  123. XML_REGEXP_SYMBOL_CURRENCY,
  124. XML_REGEXP_SYMBOL_MODIFIER,
  125. XML_REGEXP_SYMBOL_OTHERS,
  126. XML_REGEXP_OTHER,
  127. XML_REGEXP_OTHER_CONTROL,
  128. XML_REGEXP_OTHER_FORMAT,
  129. XML_REGEXP_OTHER_PRIVATE,
  130. XML_REGEXP_OTHER_NA,
  131. XML_REGEXP_BLOCK_NAME
  132. } xmlRegAtomType;
  133. typedef enum {
  134. XML_REGEXP_QUANT_EPSILON = 1,
  135. XML_REGEXP_QUANT_ONCE,
  136. XML_REGEXP_QUANT_OPT,
  137. XML_REGEXP_QUANT_MULT,
  138. XML_REGEXP_QUANT_PLUS,
  139. XML_REGEXP_QUANT_ONCEONLY,
  140. XML_REGEXP_QUANT_ALL,
  141. XML_REGEXP_QUANT_RANGE
  142. } xmlRegQuantType;
  143. typedef enum {
  144. XML_REGEXP_START_STATE = 1,
  145. XML_REGEXP_FINAL_STATE,
  146. XML_REGEXP_TRANS_STATE,
  147. XML_REGEXP_SINK_STATE,
  148. XML_REGEXP_UNREACH_STATE
  149. } xmlRegStateType;
  150. typedef enum {
  151. XML_REGEXP_MARK_NORMAL = 0,
  152. XML_REGEXP_MARK_START,
  153. XML_REGEXP_MARK_VISITED
  154. } xmlRegMarkedType;
  155. typedef struct _xmlRegRange xmlRegRange;
  156. typedef xmlRegRange *xmlRegRangePtr;
  157. struct _xmlRegRange {
  158. int neg; /* 0 normal, 1 not, 2 exclude */
  159. xmlRegAtomType type;
  160. int start;
  161. int end;
  162. xmlChar *blockName;
  163. };
  164. typedef struct _xmlRegAtom xmlRegAtom;
  165. typedef xmlRegAtom *xmlRegAtomPtr;
  166. typedef struct _xmlAutomataState xmlRegState;
  167. typedef xmlRegState *xmlRegStatePtr;
  168. struct _xmlRegAtom {
  169. int no;
  170. xmlRegAtomType type;
  171. xmlRegQuantType quant;
  172. int min;
  173. int max;
  174. void *valuep;
  175. void *valuep2;
  176. int neg;
  177. int codepoint;
  178. xmlRegStatePtr start;
  179. xmlRegStatePtr start0;
  180. xmlRegStatePtr stop;
  181. int maxRanges;
  182. int nbRanges;
  183. xmlRegRangePtr *ranges;
  184. void *data;
  185. };
  186. typedef struct _xmlRegCounter xmlRegCounter;
  187. typedef xmlRegCounter *xmlRegCounterPtr;
  188. struct _xmlRegCounter {
  189. int min;
  190. int max;
  191. };
  192. typedef struct _xmlRegTrans xmlRegTrans;
  193. typedef xmlRegTrans *xmlRegTransPtr;
  194. struct _xmlRegTrans {
  195. xmlRegAtomPtr atom;
  196. int to;
  197. int counter;
  198. int count;
  199. int nd;
  200. };
  201. struct _xmlAutomataState {
  202. xmlRegStateType type;
  203. xmlRegMarkedType mark;
  204. xmlRegMarkedType markd;
  205. xmlRegMarkedType reached;
  206. int no;
  207. int maxTrans;
  208. int nbTrans;
  209. xmlRegTrans *trans;
  210. /* knowing states pointing to us can speed things up */
  211. int maxTransTo;
  212. int nbTransTo;
  213. int *transTo;
  214. };
  215. typedef struct _xmlAutomata xmlRegParserCtxt;
  216. typedef xmlRegParserCtxt *xmlRegParserCtxtPtr;
  217. #define AM_AUTOMATA_RNG 1
  218. struct _xmlAutomata {
  219. xmlChar *string;
  220. xmlChar *cur;
  221. int error;
  222. int neg;
  223. xmlRegStatePtr start;
  224. xmlRegStatePtr end;
  225. xmlRegStatePtr state;
  226. xmlRegAtomPtr atom;
  227. int maxAtoms;
  228. int nbAtoms;
  229. xmlRegAtomPtr *atoms;
  230. int maxStates;
  231. int nbStates;
  232. xmlRegStatePtr *states;
  233. int maxCounters;
  234. int nbCounters;
  235. xmlRegCounter *counters;
  236. int determinist;
  237. int negs;
  238. int flags;
  239. int depth;
  240. };
  241. struct _xmlRegexp {
  242. xmlChar *string;
  243. int nbStates;
  244. xmlRegStatePtr *states;
  245. int nbAtoms;
  246. xmlRegAtomPtr *atoms;
  247. int nbCounters;
  248. xmlRegCounter *counters;
  249. int determinist;
  250. int flags;
  251. /*
  252. * That's the compact form for determinists automatas
  253. */
  254. int nbstates;
  255. int *compact;
  256. void **transdata;
  257. int nbstrings;
  258. xmlChar **stringMap;
  259. };
  260. typedef struct _xmlRegExecRollback xmlRegExecRollback;
  261. typedef xmlRegExecRollback *xmlRegExecRollbackPtr;
  262. struct _xmlRegExecRollback {
  263. xmlRegStatePtr state;/* the current state */
  264. int index; /* the index in the input stack */
  265. int nextbranch; /* the next transition to explore in that state */
  266. int *counts; /* save the automata state if it has some */
  267. };
  268. typedef struct _xmlRegInputToken xmlRegInputToken;
  269. typedef xmlRegInputToken *xmlRegInputTokenPtr;
  270. struct _xmlRegInputToken {
  271. xmlChar *value;
  272. void *data;
  273. };
  274. struct _xmlRegExecCtxt {
  275. int status; /* execution status != 0 indicate an error */
  276. int determinist; /* did we find an indeterministic behaviour */
  277. xmlRegexpPtr comp; /* the compiled regexp */
  278. xmlRegExecCallbacks callback;
  279. void *data;
  280. xmlRegStatePtr state;/* the current state */
  281. int transno; /* the current transition on that state */
  282. int transcount; /* the number of chars in char counted transitions */
  283. /*
  284. * A stack of rollback states
  285. */
  286. int maxRollbacks;
  287. int nbRollbacks;
  288. xmlRegExecRollback *rollbacks;
  289. /*
  290. * The state of the automata if any
  291. */
  292. int *counts;
  293. /*
  294. * The input stack
  295. */
  296. int inputStackMax;
  297. int inputStackNr;
  298. int index;
  299. int *charStack;
  300. const xmlChar *inputString; /* when operating on characters */
  301. xmlRegInputTokenPtr inputStack;/* when operating on strings */
  302. /*
  303. * error handling
  304. */
  305. int errStateNo; /* the error state number */
  306. xmlRegStatePtr errState; /* the error state */
  307. xmlChar *errString; /* the string raising the error */
  308. int *errCounts; /* counters at the error state */
  309. int nbPush;
  310. };
  311. #define REGEXP_ALL_COUNTER 0x123456
  312. #define REGEXP_ALL_LAX_COUNTER 0x123457
  313. static void xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top);
  314. static void xmlRegFreeState(xmlRegStatePtr state);
  315. static void xmlRegFreeAtom(xmlRegAtomPtr atom);
  316. static int xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr);
  317. static int xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint);
  318. static int xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint,
  319. int neg, int start, int end, const xmlChar *blockName);
  320. void xmlAutomataSetFlags(xmlAutomataPtr am, int flags);
  321. /************************************************************************
  322. * *
  323. * Regexp memory error handler *
  324. * *
  325. ************************************************************************/
  326. /**
  327. * xmlRegexpErrMemory:
  328. * @extra: extra information
  329. *
  330. * Handle an out of memory condition
  331. */
  332. static void
  333. xmlRegexpErrMemory(xmlRegParserCtxtPtr ctxt, const char *extra)
  334. {
  335. const char *regexp = NULL;
  336. if (ctxt != NULL) {
  337. regexp = (const char *) ctxt->string;
  338. ctxt->error = XML_ERR_NO_MEMORY;
  339. }
  340. __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP,
  341. XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
  342. regexp, NULL, 0, 0,
  343. "Memory allocation failed : %s\n", extra);
  344. }
  345. /**
  346. * xmlRegexpErrCompile:
  347. * @extra: extra information
  348. *
  349. * Handle a compilation failure
  350. */
  351. static void
  352. xmlRegexpErrCompile(xmlRegParserCtxtPtr ctxt, const char *extra)
  353. {
  354. const char *regexp = NULL;
  355. int idx = 0;
  356. if (ctxt != NULL) {
  357. regexp = (const char *) ctxt->string;
  358. idx = ctxt->cur - ctxt->string;
  359. ctxt->error = XML_REGEXP_COMPILE_ERROR;
  360. }
  361. __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP,
  362. XML_REGEXP_COMPILE_ERROR, XML_ERR_FATAL, NULL, 0, extra,
  363. regexp, NULL, idx, 0,
  364. "failed to compile: %s\n", extra);
  365. }
  366. /************************************************************************
  367. * *
  368. * Allocation/Deallocation *
  369. * *
  370. ************************************************************************/
  371. static int xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt);
  372. /**
  373. * xmlRegCalloc2:
  374. * @dim1: size of first dimension
  375. * @dim2: size of second dimension
  376. * @elemSize: size of element
  377. *
  378. * Allocate a two-dimensional array and set all elements to zero.
  379. *
  380. * Returns the new array or NULL in case of error.
  381. */
  382. static void*
  383. xmlRegCalloc2(size_t dim1, size_t dim2, size_t elemSize) {
  384. size_t totalSize;
  385. void *ret;
  386. /* Check for overflow */
  387. if (dim1 > SIZE_MAX / dim2 / elemSize)
  388. return (NULL);
  389. totalSize = dim1 * dim2 * elemSize;
  390. ret = xmlMalloc(totalSize);
  391. if (ret != NULL)
  392. memset(ret, 0, totalSize);
  393. return (ret);
  394. }
  395. /**
  396. * xmlRegEpxFromParse:
  397. * @ctxt: the parser context used to build it
  398. *
  399. * Allocate a new regexp and fill it with the result from the parser
  400. *
  401. * Returns the new regexp or NULL in case of error
  402. */
  403. static xmlRegexpPtr
  404. xmlRegEpxFromParse(xmlRegParserCtxtPtr ctxt) {
  405. xmlRegexpPtr ret;
  406. ret = (xmlRegexpPtr) xmlMalloc(sizeof(xmlRegexp));
  407. if (ret == NULL) {
  408. xmlRegexpErrMemory(ctxt, "compiling regexp");
  409. return(NULL);
  410. }
  411. memset(ret, 0, sizeof(xmlRegexp));
  412. ret->string = ctxt->string;
  413. ret->nbStates = ctxt->nbStates;
  414. ret->states = ctxt->states;
  415. ret->nbAtoms = ctxt->nbAtoms;
  416. ret->atoms = ctxt->atoms;
  417. ret->nbCounters = ctxt->nbCounters;
  418. ret->counters = ctxt->counters;
  419. ret->determinist = ctxt->determinist;
  420. ret->flags = ctxt->flags;
  421. if (ret->determinist == -1) {
  422. xmlRegexpIsDeterminist(ret);
  423. }
  424. if ((ret->determinist != 0) &&
  425. (ret->nbCounters == 0) &&
  426. (ctxt->negs == 0) &&
  427. (ret->atoms != NULL) &&
  428. (ret->atoms[0] != NULL) &&
  429. (ret->atoms[0]->type == XML_REGEXP_STRING)) {
  430. int i, j, nbstates = 0, nbatoms = 0;
  431. int *stateRemap;
  432. int *stringRemap;
  433. int *transitions;
  434. void **transdata;
  435. xmlChar **stringMap;
  436. xmlChar *value;
  437. /*
  438. * Switch to a compact representation
  439. * 1/ counting the effective number of states left
  440. * 2/ counting the unique number of atoms, and check that
  441. * they are all of the string type
  442. * 3/ build a table state x atom for the transitions
  443. */
  444. stateRemap = xmlMalloc(ret->nbStates * sizeof(int));
  445. if (stateRemap == NULL) {
  446. xmlRegexpErrMemory(ctxt, "compiling regexp");
  447. xmlFree(ret);
  448. return(NULL);
  449. }
  450. for (i = 0;i < ret->nbStates;i++) {
  451. if (ret->states[i] != NULL) {
  452. stateRemap[i] = nbstates;
  453. nbstates++;
  454. } else {
  455. stateRemap[i] = -1;
  456. }
  457. }
  458. #ifdef DEBUG_COMPACTION
  459. printf("Final: %d states\n", nbstates);
  460. #endif
  461. stringMap = xmlMalloc(ret->nbAtoms * sizeof(char *));
  462. if (stringMap == NULL) {
  463. xmlRegexpErrMemory(ctxt, "compiling regexp");
  464. xmlFree(stateRemap);
  465. xmlFree(ret);
  466. return(NULL);
  467. }
  468. stringRemap = xmlMalloc(ret->nbAtoms * sizeof(int));
  469. if (stringRemap == NULL) {
  470. xmlRegexpErrMemory(ctxt, "compiling regexp");
  471. xmlFree(stringMap);
  472. xmlFree(stateRemap);
  473. xmlFree(ret);
  474. return(NULL);
  475. }
  476. for (i = 0;i < ret->nbAtoms;i++) {
  477. if ((ret->atoms[i]->type == XML_REGEXP_STRING) &&
  478. (ret->atoms[i]->quant == XML_REGEXP_QUANT_ONCE)) {
  479. value = ret->atoms[i]->valuep;
  480. for (j = 0;j < nbatoms;j++) {
  481. if (xmlStrEqual(stringMap[j], value)) {
  482. stringRemap[i] = j;
  483. break;
  484. }
  485. }
  486. if (j >= nbatoms) {
  487. stringRemap[i] = nbatoms;
  488. stringMap[nbatoms] = xmlStrdup(value);
  489. if (stringMap[nbatoms] == NULL) {
  490. for (i = 0;i < nbatoms;i++)
  491. xmlFree(stringMap[i]);
  492. xmlFree(stringRemap);
  493. xmlFree(stringMap);
  494. xmlFree(stateRemap);
  495. xmlFree(ret);
  496. return(NULL);
  497. }
  498. nbatoms++;
  499. }
  500. } else {
  501. xmlFree(stateRemap);
  502. xmlFree(stringRemap);
  503. for (i = 0;i < nbatoms;i++)
  504. xmlFree(stringMap[i]);
  505. xmlFree(stringMap);
  506. xmlFree(ret);
  507. return(NULL);
  508. }
  509. }
  510. #ifdef DEBUG_COMPACTION
  511. printf("Final: %d atoms\n", nbatoms);
  512. #endif
  513. transitions = (int *) xmlRegCalloc2(nbstates + 1, nbatoms + 1,
  514. sizeof(int));
  515. if (transitions == NULL) {
  516. xmlFree(stateRemap);
  517. xmlFree(stringRemap);
  518. for (i = 0;i < nbatoms;i++)
  519. xmlFree(stringMap[i]);
  520. xmlFree(stringMap);
  521. xmlFree(ret);
  522. return(NULL);
  523. }
  524. /*
  525. * Allocate the transition table. The first entry for each
  526. * state corresponds to the state type.
  527. */
  528. transdata = NULL;
  529. for (i = 0;i < ret->nbStates;i++) {
  530. int stateno, atomno, targetno, prev;
  531. xmlRegStatePtr state;
  532. xmlRegTransPtr trans;
  533. stateno = stateRemap[i];
  534. if (stateno == -1)
  535. continue;
  536. state = ret->states[i];
  537. transitions[stateno * (nbatoms + 1)] = state->type;
  538. for (j = 0;j < state->nbTrans;j++) {
  539. trans = &(state->trans[j]);
  540. if ((trans->to == -1) || (trans->atom == NULL))
  541. continue;
  542. atomno = stringRemap[trans->atom->no];
  543. if ((trans->atom->data != NULL) && (transdata == NULL)) {
  544. transdata = (void **) xmlRegCalloc2(nbstates, nbatoms,
  545. sizeof(void *));
  546. if (transdata == NULL) {
  547. xmlRegexpErrMemory(ctxt, "compiling regexp");
  548. break;
  549. }
  550. }
  551. targetno = stateRemap[trans->to];
  552. /*
  553. * if the same atom can generate transitions to 2 different
  554. * states then it means the automata is not deterministic and
  555. * the compact form can't be used !
  556. */
  557. prev = transitions[stateno * (nbatoms + 1) + atomno + 1];
  558. if (prev != 0) {
  559. if (prev != targetno + 1) {
  560. ret->determinist = 0;
  561. #ifdef DEBUG_COMPACTION
  562. printf("Indet: state %d trans %d, atom %d to %d : %d to %d\n",
  563. i, j, trans->atom->no, trans->to, atomno, targetno);
  564. printf(" previous to is %d\n", prev);
  565. #endif
  566. if (transdata != NULL)
  567. xmlFree(transdata);
  568. xmlFree(transitions);
  569. xmlFree(stateRemap);
  570. xmlFree(stringRemap);
  571. for (i = 0;i < nbatoms;i++)
  572. xmlFree(stringMap[i]);
  573. xmlFree(stringMap);
  574. goto not_determ;
  575. }
  576. } else {
  577. #if 0
  578. printf("State %d trans %d: atom %d to %d : %d to %d\n",
  579. i, j, trans->atom->no, trans->to, atomno, targetno);
  580. #endif
  581. transitions[stateno * (nbatoms + 1) + atomno + 1] =
  582. targetno + 1; /* to avoid 0 */
  583. if (transdata != NULL)
  584. transdata[stateno * nbatoms + atomno] =
  585. trans->atom->data;
  586. }
  587. }
  588. }
  589. ret->determinist = 1;
  590. #ifdef DEBUG_COMPACTION
  591. /*
  592. * Debug
  593. */
  594. for (i = 0;i < nbstates;i++) {
  595. for (j = 0;j < nbatoms + 1;j++) {
  596. printf("%02d ", transitions[i * (nbatoms + 1) + j]);
  597. }
  598. printf("\n");
  599. }
  600. printf("\n");
  601. #endif
  602. /*
  603. * Cleanup of the old data
  604. */
  605. if (ret->states != NULL) {
  606. for (i = 0;i < ret->nbStates;i++)
  607. xmlRegFreeState(ret->states[i]);
  608. xmlFree(ret->states);
  609. }
  610. ret->states = NULL;
  611. ret->nbStates = 0;
  612. if (ret->atoms != NULL) {
  613. for (i = 0;i < ret->nbAtoms;i++)
  614. xmlRegFreeAtom(ret->atoms[i]);
  615. xmlFree(ret->atoms);
  616. }
  617. ret->atoms = NULL;
  618. ret->nbAtoms = 0;
  619. ret->compact = transitions;
  620. ret->transdata = transdata;
  621. ret->stringMap = stringMap;
  622. ret->nbstrings = nbatoms;
  623. ret->nbstates = nbstates;
  624. xmlFree(stateRemap);
  625. xmlFree(stringRemap);
  626. }
  627. not_determ:
  628. ctxt->string = NULL;
  629. ctxt->nbStates = 0;
  630. ctxt->states = NULL;
  631. ctxt->nbAtoms = 0;
  632. ctxt->atoms = NULL;
  633. ctxt->nbCounters = 0;
  634. ctxt->counters = NULL;
  635. return(ret);
  636. }
  637. /**
  638. * xmlRegNewParserCtxt:
  639. * @string: the string to parse
  640. *
  641. * Allocate a new regexp parser context
  642. *
  643. * Returns the new context or NULL in case of error
  644. */
  645. static xmlRegParserCtxtPtr
  646. xmlRegNewParserCtxt(const xmlChar *string) {
  647. xmlRegParserCtxtPtr ret;
  648. ret = (xmlRegParserCtxtPtr) xmlMalloc(sizeof(xmlRegParserCtxt));
  649. if (ret == NULL)
  650. return(NULL);
  651. memset(ret, 0, sizeof(xmlRegParserCtxt));
  652. if (string != NULL)
  653. ret->string = xmlStrdup(string);
  654. ret->cur = ret->string;
  655. ret->neg = 0;
  656. ret->negs = 0;
  657. ret->error = 0;
  658. ret->determinist = -1;
  659. return(ret);
  660. }
  661. /**
  662. * xmlRegNewRange:
  663. * @ctxt: the regexp parser context
  664. * @neg: is that negative
  665. * @type: the type of range
  666. * @start: the start codepoint
  667. * @end: the end codepoint
  668. *
  669. * Allocate a new regexp range
  670. *
  671. * Returns the new range or NULL in case of error
  672. */
  673. static xmlRegRangePtr
  674. xmlRegNewRange(xmlRegParserCtxtPtr ctxt,
  675. int neg, xmlRegAtomType type, int start, int end) {
  676. xmlRegRangePtr ret;
  677. ret = (xmlRegRangePtr) xmlMalloc(sizeof(xmlRegRange));
  678. if (ret == NULL) {
  679. xmlRegexpErrMemory(ctxt, "allocating range");
  680. return(NULL);
  681. }
  682. ret->neg = neg;
  683. ret->type = type;
  684. ret->start = start;
  685. ret->end = end;
  686. return(ret);
  687. }
  688. /**
  689. * xmlRegFreeRange:
  690. * @range: the regexp range
  691. *
  692. * Free a regexp range
  693. */
  694. static void
  695. xmlRegFreeRange(xmlRegRangePtr range) {
  696. if (range == NULL)
  697. return;
  698. if (range->blockName != NULL)
  699. xmlFree(range->blockName);
  700. xmlFree(range);
  701. }
  702. /**
  703. * xmlRegCopyRange:
  704. * @range: the regexp range
  705. *
  706. * Copy a regexp range
  707. *
  708. * Returns the new copy or NULL in case of error.
  709. */
  710. static xmlRegRangePtr
  711. xmlRegCopyRange(xmlRegParserCtxtPtr ctxt, xmlRegRangePtr range) {
  712. xmlRegRangePtr ret;
  713. if (range == NULL)
  714. return(NULL);
  715. ret = xmlRegNewRange(ctxt, range->neg, range->type, range->start,
  716. range->end);
  717. if (ret == NULL)
  718. return(NULL);
  719. if (range->blockName != NULL) {
  720. ret->blockName = xmlStrdup(range->blockName);
  721. if (ret->blockName == NULL) {
  722. xmlRegexpErrMemory(ctxt, "allocating range");
  723. xmlRegFreeRange(ret);
  724. return(NULL);
  725. }
  726. }
  727. return(ret);
  728. }
  729. /**
  730. * xmlRegNewAtom:
  731. * @ctxt: the regexp parser context
  732. * @type: the type of atom
  733. *
  734. * Allocate a new atom
  735. *
  736. * Returns the new atom or NULL in case of error
  737. */
  738. static xmlRegAtomPtr
  739. xmlRegNewAtom(xmlRegParserCtxtPtr ctxt, xmlRegAtomType type) {
  740. xmlRegAtomPtr ret;
  741. ret = (xmlRegAtomPtr) xmlMalloc(sizeof(xmlRegAtom));
  742. if (ret == NULL) {
  743. xmlRegexpErrMemory(ctxt, "allocating atom");
  744. return(NULL);
  745. }
  746. memset(ret, 0, sizeof(xmlRegAtom));
  747. ret->type = type;
  748. ret->quant = XML_REGEXP_QUANT_ONCE;
  749. ret->min = 0;
  750. ret->max = 0;
  751. return(ret);
  752. }
  753. /**
  754. * xmlRegFreeAtom:
  755. * @atom: the regexp atom
  756. *
  757. * Free a regexp atom
  758. */
  759. static void
  760. xmlRegFreeAtom(xmlRegAtomPtr atom) {
  761. int i;
  762. if (atom == NULL)
  763. return;
  764. for (i = 0;i < atom->nbRanges;i++)
  765. xmlRegFreeRange(atom->ranges[i]);
  766. if (atom->ranges != NULL)
  767. xmlFree(atom->ranges);
  768. if ((atom->type == XML_REGEXP_STRING) && (atom->valuep != NULL))
  769. xmlFree(atom->valuep);
  770. if ((atom->type == XML_REGEXP_STRING) && (atom->valuep2 != NULL))
  771. xmlFree(atom->valuep2);
  772. if ((atom->type == XML_REGEXP_BLOCK_NAME) && (atom->valuep != NULL))
  773. xmlFree(atom->valuep);
  774. xmlFree(atom);
  775. }
  776. /**
  777. * xmlRegCopyAtom:
  778. * @ctxt: the regexp parser context
  779. * @atom: the original atom
  780. *
  781. * Allocate a new regexp range
  782. *
  783. * Returns the new atom or NULL in case of error
  784. */
  785. static xmlRegAtomPtr
  786. xmlRegCopyAtom(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom) {
  787. xmlRegAtomPtr ret;
  788. ret = (xmlRegAtomPtr) xmlMalloc(sizeof(xmlRegAtom));
  789. if (ret == NULL) {
  790. xmlRegexpErrMemory(ctxt, "copying atom");
  791. return(NULL);
  792. }
  793. memset(ret, 0, sizeof(xmlRegAtom));
  794. ret->type = atom->type;
  795. ret->quant = atom->quant;
  796. ret->min = atom->min;
  797. ret->max = atom->max;
  798. if (atom->nbRanges > 0) {
  799. int i;
  800. ret->ranges = (xmlRegRangePtr *) xmlMalloc(sizeof(xmlRegRangePtr) *
  801. atom->nbRanges);
  802. if (ret->ranges == NULL) {
  803. xmlRegexpErrMemory(ctxt, "copying atom");
  804. goto error;
  805. }
  806. for (i = 0;i < atom->nbRanges;i++) {
  807. ret->ranges[i] = xmlRegCopyRange(ctxt, atom->ranges[i]);
  808. if (ret->ranges[i] == NULL)
  809. goto error;
  810. ret->nbRanges = i + 1;
  811. }
  812. }
  813. return(ret);
  814. error:
  815. xmlRegFreeAtom(ret);
  816. return(NULL);
  817. }
  818. static xmlRegStatePtr
  819. xmlRegNewState(xmlRegParserCtxtPtr ctxt) {
  820. xmlRegStatePtr ret;
  821. ret = (xmlRegStatePtr) xmlMalloc(sizeof(xmlRegState));
  822. if (ret == NULL) {
  823. xmlRegexpErrMemory(ctxt, "allocating state");
  824. return(NULL);
  825. }
  826. memset(ret, 0, sizeof(xmlRegState));
  827. ret->type = XML_REGEXP_TRANS_STATE;
  828. ret->mark = XML_REGEXP_MARK_NORMAL;
  829. return(ret);
  830. }
  831. /**
  832. * xmlRegFreeState:
  833. * @state: the regexp state
  834. *
  835. * Free a regexp state
  836. */
  837. static void
  838. xmlRegFreeState(xmlRegStatePtr state) {
  839. if (state == NULL)
  840. return;
  841. if (state->trans != NULL)
  842. xmlFree(state->trans);
  843. if (state->transTo != NULL)
  844. xmlFree(state->transTo);
  845. xmlFree(state);
  846. }
  847. /**
  848. * xmlRegFreeParserCtxt:
  849. * @ctxt: the regexp parser context
  850. *
  851. * Free a regexp parser context
  852. */
  853. static void
  854. xmlRegFreeParserCtxt(xmlRegParserCtxtPtr ctxt) {
  855. int i;
  856. if (ctxt == NULL)
  857. return;
  858. if (ctxt->string != NULL)
  859. xmlFree(ctxt->string);
  860. if (ctxt->states != NULL) {
  861. for (i = 0;i < ctxt->nbStates;i++)
  862. xmlRegFreeState(ctxt->states[i]);
  863. xmlFree(ctxt->states);
  864. }
  865. if (ctxt->atoms != NULL) {
  866. for (i = 0;i < ctxt->nbAtoms;i++)
  867. xmlRegFreeAtom(ctxt->atoms[i]);
  868. xmlFree(ctxt->atoms);
  869. }
  870. if (ctxt->counters != NULL)
  871. xmlFree(ctxt->counters);
  872. xmlFree(ctxt);
  873. }
  874. /************************************************************************
  875. * *
  876. * Display of Data structures *
  877. * *
  878. ************************************************************************/
  879. static void
  880. xmlRegPrintAtomType(FILE *output, xmlRegAtomType type) {
  881. switch (type) {
  882. case XML_REGEXP_EPSILON:
  883. fprintf(output, "epsilon "); break;
  884. case XML_REGEXP_CHARVAL:
  885. fprintf(output, "charval "); break;
  886. case XML_REGEXP_RANGES:
  887. fprintf(output, "ranges "); break;
  888. case XML_REGEXP_SUBREG:
  889. fprintf(output, "subexpr "); break;
  890. case XML_REGEXP_STRING:
  891. fprintf(output, "string "); break;
  892. case XML_REGEXP_ANYCHAR:
  893. fprintf(output, "anychar "); break;
  894. case XML_REGEXP_ANYSPACE:
  895. fprintf(output, "anyspace "); break;
  896. case XML_REGEXP_NOTSPACE:
  897. fprintf(output, "notspace "); break;
  898. case XML_REGEXP_INITNAME:
  899. fprintf(output, "initname "); break;
  900. case XML_REGEXP_NOTINITNAME:
  901. fprintf(output, "notinitname "); break;
  902. case XML_REGEXP_NAMECHAR:
  903. fprintf(output, "namechar "); break;
  904. case XML_REGEXP_NOTNAMECHAR:
  905. fprintf(output, "notnamechar "); break;
  906. case XML_REGEXP_DECIMAL:
  907. fprintf(output, "decimal "); break;
  908. case XML_REGEXP_NOTDECIMAL:
  909. fprintf(output, "notdecimal "); break;
  910. case XML_REGEXP_REALCHAR:
  911. fprintf(output, "realchar "); break;
  912. case XML_REGEXP_NOTREALCHAR:
  913. fprintf(output, "notrealchar "); break;
  914. case XML_REGEXP_LETTER:
  915. fprintf(output, "LETTER "); break;
  916. case XML_REGEXP_LETTER_UPPERCASE:
  917. fprintf(output, "LETTER_UPPERCASE "); break;
  918. case XML_REGEXP_LETTER_LOWERCASE:
  919. fprintf(output, "LETTER_LOWERCASE "); break;
  920. case XML_REGEXP_LETTER_TITLECASE:
  921. fprintf(output, "LETTER_TITLECASE "); break;
  922. case XML_REGEXP_LETTER_MODIFIER:
  923. fprintf(output, "LETTER_MODIFIER "); break;
  924. case XML_REGEXP_LETTER_OTHERS:
  925. fprintf(output, "LETTER_OTHERS "); break;
  926. case XML_REGEXP_MARK:
  927. fprintf(output, "MARK "); break;
  928. case XML_REGEXP_MARK_NONSPACING:
  929. fprintf(output, "MARK_NONSPACING "); break;
  930. case XML_REGEXP_MARK_SPACECOMBINING:
  931. fprintf(output, "MARK_SPACECOMBINING "); break;
  932. case XML_REGEXP_MARK_ENCLOSING:
  933. fprintf(output, "MARK_ENCLOSING "); break;
  934. case XML_REGEXP_NUMBER:
  935. fprintf(output, "NUMBER "); break;
  936. case XML_REGEXP_NUMBER_DECIMAL:
  937. fprintf(output, "NUMBER_DECIMAL "); break;
  938. case XML_REGEXP_NUMBER_LETTER:
  939. fprintf(output, "NUMBER_LETTER "); break;
  940. case XML_REGEXP_NUMBER_OTHERS:
  941. fprintf(output, "NUMBER_OTHERS "); break;
  942. case XML_REGEXP_PUNCT:
  943. fprintf(output, "PUNCT "); break;
  944. case XML_REGEXP_PUNCT_CONNECTOR:
  945. fprintf(output, "PUNCT_CONNECTOR "); break;
  946. case XML_REGEXP_PUNCT_DASH:
  947. fprintf(output, "PUNCT_DASH "); break;
  948. case XML_REGEXP_PUNCT_OPEN:
  949. fprintf(output, "PUNCT_OPEN "); break;
  950. case XML_REGEXP_PUNCT_CLOSE:
  951. fprintf(output, "PUNCT_CLOSE "); break;
  952. case XML_REGEXP_PUNCT_INITQUOTE:
  953. fprintf(output, "PUNCT_INITQUOTE "); break;
  954. case XML_REGEXP_PUNCT_FINQUOTE:
  955. fprintf(output, "PUNCT_FINQUOTE "); break;
  956. case XML_REGEXP_PUNCT_OTHERS:
  957. fprintf(output, "PUNCT_OTHERS "); break;
  958. case XML_REGEXP_SEPAR:
  959. fprintf(output, "SEPAR "); break;
  960. case XML_REGEXP_SEPAR_SPACE:
  961. fprintf(output, "SEPAR_SPACE "); break;
  962. case XML_REGEXP_SEPAR_LINE:
  963. fprintf(output, "SEPAR_LINE "); break;
  964. case XML_REGEXP_SEPAR_PARA:
  965. fprintf(output, "SEPAR_PARA "); break;
  966. case XML_REGEXP_SYMBOL:
  967. fprintf(output, "SYMBOL "); break;
  968. case XML_REGEXP_SYMBOL_MATH:
  969. fprintf(output, "SYMBOL_MATH "); break;
  970. case XML_REGEXP_SYMBOL_CURRENCY:
  971. fprintf(output, "SYMBOL_CURRENCY "); break;
  972. case XML_REGEXP_SYMBOL_MODIFIER:
  973. fprintf(output, "SYMBOL_MODIFIER "); break;
  974. case XML_REGEXP_SYMBOL_OTHERS:
  975. fprintf(output, "SYMBOL_OTHERS "); break;
  976. case XML_REGEXP_OTHER:
  977. fprintf(output, "OTHER "); break;
  978. case XML_REGEXP_OTHER_CONTROL:
  979. fprintf(output, "OTHER_CONTROL "); break;
  980. case XML_REGEXP_OTHER_FORMAT:
  981. fprintf(output, "OTHER_FORMAT "); break;
  982. case XML_REGEXP_OTHER_PRIVATE:
  983. fprintf(output, "OTHER_PRIVATE "); break;
  984. case XML_REGEXP_OTHER_NA:
  985. fprintf(output, "OTHER_NA "); break;
  986. case XML_REGEXP_BLOCK_NAME:
  987. fprintf(output, "BLOCK "); break;
  988. }
  989. }
  990. static void
  991. xmlRegPrintQuantType(FILE *output, xmlRegQuantType type) {
  992. switch (type) {
  993. case XML_REGEXP_QUANT_EPSILON:
  994. fprintf(output, "epsilon "); break;
  995. case XML_REGEXP_QUANT_ONCE:
  996. fprintf(output, "once "); break;
  997. case XML_REGEXP_QUANT_OPT:
  998. fprintf(output, "? "); break;
  999. case XML_REGEXP_QUANT_MULT:
  1000. fprintf(output, "* "); break;
  1001. case XML_REGEXP_QUANT_PLUS:
  1002. fprintf(output, "+ "); break;
  1003. case XML_REGEXP_QUANT_RANGE:
  1004. fprintf(output, "range "); break;
  1005. case XML_REGEXP_QUANT_ONCEONLY:
  1006. fprintf(output, "onceonly "); break;
  1007. case XML_REGEXP_QUANT_ALL:
  1008. fprintf(output, "all "); break;
  1009. }
  1010. }
  1011. static void
  1012. xmlRegPrintRange(FILE *output, xmlRegRangePtr range) {
  1013. fprintf(output, " range: ");
  1014. if (range->neg)
  1015. fprintf(output, "negative ");
  1016. xmlRegPrintAtomType(output, range->type);
  1017. fprintf(output, "%c - %c\n", range->start, range->end);
  1018. }
  1019. static void
  1020. xmlRegPrintAtom(FILE *output, xmlRegAtomPtr atom) {
  1021. fprintf(output, " atom: ");
  1022. if (atom == NULL) {
  1023. fprintf(output, "NULL\n");
  1024. return;
  1025. }
  1026. if (atom->neg)
  1027. fprintf(output, "not ");
  1028. xmlRegPrintAtomType(output, atom->type);
  1029. xmlRegPrintQuantType(output, atom->quant);
  1030. if (atom->quant == XML_REGEXP_QUANT_RANGE)
  1031. fprintf(output, "%d-%d ", atom->min, atom->max);
  1032. if (atom->type == XML_REGEXP_STRING)
  1033. fprintf(output, "'%s' ", (char *) atom->valuep);
  1034. if (atom->type == XML_REGEXP_CHARVAL)
  1035. fprintf(output, "char %c\n", atom->codepoint);
  1036. else if (atom->type == XML_REGEXP_RANGES) {
  1037. int i;
  1038. fprintf(output, "%d entries\n", atom->nbRanges);
  1039. for (i = 0; i < atom->nbRanges;i++)
  1040. xmlRegPrintRange(output, atom->ranges[i]);
  1041. } else if (atom->type == XML_REGEXP_SUBREG) {
  1042. fprintf(output, "start %d end %d\n", atom->start->no, atom->stop->no);
  1043. } else {
  1044. fprintf(output, "\n");
  1045. }
  1046. }
  1047. static void
  1048. xmlRegPrintTrans(FILE *output, xmlRegTransPtr trans) {
  1049. fprintf(output, " trans: ");
  1050. if (trans == NULL) {
  1051. fprintf(output, "NULL\n");
  1052. return;
  1053. }
  1054. if (trans->to < 0) {
  1055. fprintf(output, "removed\n");
  1056. return;
  1057. }
  1058. if (trans->nd != 0) {
  1059. if (trans->nd == 2)
  1060. fprintf(output, "last not determinist, ");
  1061. else
  1062. fprintf(output, "not determinist, ");
  1063. }
  1064. if (trans->counter >= 0) {
  1065. fprintf(output, "counted %d, ", trans->counter);
  1066. }
  1067. if (trans->count == REGEXP_ALL_COUNTER) {
  1068. fprintf(output, "all transition, ");
  1069. } else if (trans->count >= 0) {
  1070. fprintf(output, "count based %d, ", trans->count);
  1071. }
  1072. if (trans->atom == NULL) {
  1073. fprintf(output, "epsilon to %d\n", trans->to);
  1074. return;
  1075. }
  1076. if (trans->atom->type == XML_REGEXP_CHARVAL)
  1077. fprintf(output, "char %c ", trans->atom->codepoint);
  1078. fprintf(output, "atom %d, to %d\n", trans->atom->no, trans->to);
  1079. }
  1080. static void
  1081. xmlRegPrintState(FILE *output, xmlRegStatePtr state) {
  1082. int i;
  1083. fprintf(output, " state: ");
  1084. if (state == NULL) {
  1085. fprintf(output, "NULL\n");
  1086. return;
  1087. }
  1088. if (state->type == XML_REGEXP_START_STATE)
  1089. fprintf(output, "START ");
  1090. if (state->type == XML_REGEXP_FINAL_STATE)
  1091. fprintf(output, "FINAL ");
  1092. fprintf(output, "%d, %d transitions:\n", state->no, state->nbTrans);
  1093. for (i = 0;i < state->nbTrans; i++) {
  1094. xmlRegPrintTrans(output, &(state->trans[i]));
  1095. }
  1096. }
  1097. #ifdef DEBUG_REGEXP_GRAPH
  1098. static void
  1099. xmlRegPrintCtxt(FILE *output, xmlRegParserCtxtPtr ctxt) {
  1100. int i;
  1101. fprintf(output, " ctxt: ");
  1102. if (ctxt == NULL) {
  1103. fprintf(output, "NULL\n");
  1104. return;
  1105. }
  1106. fprintf(output, "'%s' ", ctxt->string);
  1107. if (ctxt->error)
  1108. fprintf(output, "error ");
  1109. if (ctxt->neg)
  1110. fprintf(output, "neg ");
  1111. fprintf(output, "\n");
  1112. fprintf(output, "%d atoms:\n", ctxt->nbAtoms);
  1113. for (i = 0;i < ctxt->nbAtoms; i++) {
  1114. fprintf(output, " %02d ", i);
  1115. xmlRegPrintAtom(output, ctxt->atoms[i]);
  1116. }
  1117. if (ctxt->atom != NULL) {
  1118. fprintf(output, "current atom:\n");
  1119. xmlRegPrintAtom(output, ctxt->atom);
  1120. }
  1121. fprintf(output, "%d states:", ctxt->nbStates);
  1122. if (ctxt->start != NULL)
  1123. fprintf(output, " start: %d", ctxt->start->no);
  1124. if (ctxt->end != NULL)
  1125. fprintf(output, " end: %d", ctxt->end->no);
  1126. fprintf(output, "\n");
  1127. for (i = 0;i < ctxt->nbStates; i++) {
  1128. xmlRegPrintState(output, ctxt->states[i]);
  1129. }
  1130. fprintf(output, "%d counters:\n", ctxt->nbCounters);
  1131. for (i = 0;i < ctxt->nbCounters; i++) {
  1132. fprintf(output, " %d: min %d max %d\n", i, ctxt->counters[i].min,
  1133. ctxt->counters[i].max);
  1134. }
  1135. }
  1136. #endif
  1137. /************************************************************************
  1138. * *
  1139. * Finite Automata structures manipulations *
  1140. * *
  1141. ************************************************************************/
  1142. static void
  1143. xmlRegAtomAddRange(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom,
  1144. int neg, xmlRegAtomType type, int start, int end,
  1145. xmlChar *blockName) {
  1146. xmlRegRangePtr range;
  1147. if (atom == NULL) {
  1148. ERROR("add range: atom is NULL");
  1149. return;
  1150. }
  1151. if (atom->type != XML_REGEXP_RANGES) {
  1152. ERROR("add range: atom is not ranges");
  1153. return;
  1154. }
  1155. if (atom->maxRanges == 0) {
  1156. atom->maxRanges = 4;
  1157. atom->ranges = (xmlRegRangePtr *) xmlMalloc(atom->maxRanges *
  1158. sizeof(xmlRegRangePtr));
  1159. if (atom->ranges == NULL) {
  1160. xmlRegexpErrMemory(ctxt, "adding ranges");
  1161. atom->maxRanges = 0;
  1162. return;
  1163. }
  1164. } else if (atom->nbRanges >= atom->maxRanges) {
  1165. xmlRegRangePtr *tmp;
  1166. atom->maxRanges *= 2;
  1167. tmp = (xmlRegRangePtr *) xmlRealloc(atom->ranges, atom->maxRanges *
  1168. sizeof(xmlRegRangePtr));
  1169. if (tmp == NULL) {
  1170. xmlRegexpErrMemory(ctxt, "adding ranges");
  1171. atom->maxRanges /= 2;
  1172. return;
  1173. }
  1174. atom->ranges = tmp;
  1175. }
  1176. range = xmlRegNewRange(ctxt, neg, type, start, end);
  1177. if (range == NULL)
  1178. return;
  1179. range->blockName = blockName;
  1180. atom->ranges[atom->nbRanges++] = range;
  1181. }
  1182. static int
  1183. xmlRegGetCounter(xmlRegParserCtxtPtr ctxt) {
  1184. if (ctxt->maxCounters == 0) {
  1185. ctxt->maxCounters = 4;
  1186. ctxt->counters = (xmlRegCounter *) xmlMalloc(ctxt->maxCounters *
  1187. sizeof(xmlRegCounter));
  1188. if (ctxt->counters == NULL) {
  1189. xmlRegexpErrMemory(ctxt, "allocating counter");
  1190. ctxt->maxCounters = 0;
  1191. return(-1);
  1192. }
  1193. } else if (ctxt->nbCounters >= ctxt->maxCounters) {
  1194. xmlRegCounter *tmp;
  1195. ctxt->maxCounters *= 2;
  1196. tmp = (xmlRegCounter *) xmlRealloc(ctxt->counters, ctxt->maxCounters *
  1197. sizeof(xmlRegCounter));
  1198. if (tmp == NULL) {
  1199. xmlRegexpErrMemory(ctxt, "allocating counter");
  1200. ctxt->maxCounters /= 2;
  1201. return(-1);
  1202. }
  1203. ctxt->counters = tmp;
  1204. }
  1205. ctxt->counters[ctxt->nbCounters].min = -1;
  1206. ctxt->counters[ctxt->nbCounters].max = -1;
  1207. return(ctxt->nbCounters++);
  1208. }
  1209. static int
  1210. xmlRegAtomPush(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom) {
  1211. if (atom == NULL) {
  1212. ERROR("atom push: atom is NULL");
  1213. return(-1);
  1214. }
  1215. if (ctxt->maxAtoms == 0) {
  1216. ctxt->maxAtoms = 4;
  1217. ctxt->atoms = (xmlRegAtomPtr *) xmlMalloc(ctxt->maxAtoms *
  1218. sizeof(xmlRegAtomPtr));
  1219. if (ctxt->atoms == NULL) {
  1220. xmlRegexpErrMemory(ctxt, "pushing atom");
  1221. ctxt->maxAtoms = 0;
  1222. return(-1);
  1223. }
  1224. } else if (ctxt->nbAtoms >= ctxt->maxAtoms) {
  1225. xmlRegAtomPtr *tmp;
  1226. ctxt->maxAtoms *= 2;
  1227. tmp = (xmlRegAtomPtr *) xmlRealloc(ctxt->atoms, ctxt->maxAtoms *
  1228. sizeof(xmlRegAtomPtr));
  1229. if (tmp == NULL) {
  1230. xmlRegexpErrMemory(ctxt, "allocating counter");
  1231. ctxt->maxAtoms /= 2;
  1232. return(-1);
  1233. }
  1234. ctxt->atoms = tmp;
  1235. }
  1236. atom->no = ctxt->nbAtoms;
  1237. ctxt->atoms[ctxt->nbAtoms++] = atom;
  1238. return(0);
  1239. }
  1240. static void
  1241. xmlRegStateAddTransTo(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr target,
  1242. int from) {
  1243. if (target->maxTransTo == 0) {
  1244. target->maxTransTo = 8;
  1245. target->transTo = (int *) xmlMalloc(target->maxTransTo *
  1246. sizeof(int));
  1247. if (target->transTo == NULL) {
  1248. xmlRegexpErrMemory(ctxt, "adding transition");
  1249. target->maxTransTo = 0;
  1250. return;
  1251. }
  1252. } else if (target->nbTransTo >= target->maxTransTo) {
  1253. int *tmp;
  1254. target->maxTransTo *= 2;
  1255. tmp = (int *) xmlRealloc(target->transTo, target->maxTransTo *
  1256. sizeof(int));
  1257. if (tmp == NULL) {
  1258. xmlRegexpErrMemory(ctxt, "adding transition");
  1259. target->maxTransTo /= 2;
  1260. return;
  1261. }
  1262. target->transTo = tmp;
  1263. }
  1264. target->transTo[target->nbTransTo] = from;
  1265. target->nbTransTo++;
  1266. }
  1267. static void
  1268. xmlRegStateAddTrans(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
  1269. xmlRegAtomPtr atom, xmlRegStatePtr target,
  1270. int counter, int count) {
  1271. int nrtrans;
  1272. if (state == NULL) {
  1273. ERROR("add state: state is NULL");
  1274. return;
  1275. }
  1276. if (target == NULL) {
  1277. ERROR("add state: target is NULL");
  1278. return;
  1279. }
  1280. /*
  1281. * Other routines follow the philosophy 'When in doubt, add a transition'
  1282. * so we check here whether such a transition is already present and, if
  1283. * so, silently ignore this request.
  1284. */
  1285. for (nrtrans = state->nbTrans - 1; nrtrans >= 0; nrtrans--) {
  1286. xmlRegTransPtr trans = &(state->trans[nrtrans]);
  1287. if ((trans->atom == atom) &&
  1288. (trans->to == target->no) &&
  1289. (trans->counter == counter) &&
  1290. (trans->count == count)) {
  1291. #ifdef DEBUG_REGEXP_GRAPH
  1292. printf("Ignoring duplicate transition from %d to %d\n",
  1293. state->no, target->no);
  1294. #endif
  1295. return;
  1296. }
  1297. }
  1298. if (state->maxTrans == 0) {
  1299. state->maxTrans = 8;
  1300. state->trans = (xmlRegTrans *) xmlMalloc(state->maxTrans *
  1301. sizeof(xmlRegTrans));
  1302. if (state->trans == NULL) {
  1303. xmlRegexpErrMemory(ctxt, "adding transition");
  1304. state->maxTrans = 0;
  1305. return;
  1306. }
  1307. } else if (state->nbTrans >= state->maxTrans) {
  1308. xmlRegTrans *tmp;
  1309. state->maxTrans *= 2;
  1310. tmp = (xmlRegTrans *) xmlRealloc(state->trans, state->maxTrans *
  1311. sizeof(xmlRegTrans));
  1312. if (tmp == NULL) {
  1313. xmlRegexpErrMemory(ctxt, "adding transition");
  1314. state->maxTrans /= 2;
  1315. return;
  1316. }
  1317. state->trans = tmp;
  1318. }
  1319. #ifdef DEBUG_REGEXP_GRAPH
  1320. printf("Add trans from %d to %d ", state->no, target->no);
  1321. if (count == REGEXP_ALL_COUNTER)
  1322. printf("all transition\n");
  1323. else if (count >= 0)
  1324. printf("count based %d\n", count);
  1325. else if (counter >= 0)
  1326. printf("counted %d\n", counter);
  1327. else if (atom == NULL)
  1328. printf("epsilon transition\n");
  1329. else if (atom != NULL)
  1330. xmlRegPrintAtom(stdout, atom);
  1331. #endif
  1332. state->trans[state->nbTrans].atom = atom;
  1333. state->trans[state->nbTrans].to = target->no;
  1334. state->trans[state->nbTrans].counter = counter;
  1335. state->trans[state->nbTrans].count = count;
  1336. state->trans[state->nbTrans].nd = 0;
  1337. state->nbTrans++;
  1338. xmlRegStateAddTransTo(ctxt, target, state->no);
  1339. }
  1340. static int
  1341. xmlRegStatePush(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state) {
  1342. if (state == NULL) return(-1);
  1343. if (ctxt->maxStates == 0) {
  1344. ctxt->maxStates = 4;
  1345. ctxt->states = (xmlRegStatePtr *) xmlMalloc(ctxt->maxStates *
  1346. sizeof(xmlRegStatePtr));
  1347. if (ctxt->states == NULL) {
  1348. xmlRegexpErrMemory(ctxt, "adding state");
  1349. ctxt->maxStates = 0;
  1350. return(-1);
  1351. }
  1352. } else if (ctxt->nbStates >= ctxt->maxStates) {
  1353. xmlRegStatePtr *tmp;
  1354. ctxt->maxStates *= 2;
  1355. tmp = (xmlRegStatePtr *) xmlRealloc(ctxt->states, ctxt->maxStates *
  1356. sizeof(xmlRegStatePtr));
  1357. if (tmp == NULL) {
  1358. xmlRegexpErrMemory(ctxt, "adding state");
  1359. ctxt->maxStates /= 2;
  1360. return(-1);
  1361. }
  1362. ctxt->states = tmp;
  1363. }
  1364. state->no = ctxt->nbStates;
  1365. ctxt->states[ctxt->nbStates++] = state;
  1366. return(0);
  1367. }
  1368. /**
  1369. * xmlFAGenerateAllTransition:
  1370. * @ctxt: a regexp parser context
  1371. * @from: the from state
  1372. * @to: the target state or NULL for building a new one
  1373. * @lax:
  1374. *
  1375. */
  1376. static void
  1377. xmlFAGenerateAllTransition(xmlRegParserCtxtPtr ctxt,
  1378. xmlRegStatePtr from, xmlRegStatePtr to,
  1379. int lax) {
  1380. if (to == NULL) {
  1381. to = xmlRegNewState(ctxt);
  1382. xmlRegStatePush(ctxt, to);
  1383. ctxt->state = to;
  1384. }
  1385. if (lax)
  1386. xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_LAX_COUNTER);
  1387. else
  1388. xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_COUNTER);
  1389. }
  1390. /**
  1391. * xmlFAGenerateEpsilonTransition:
  1392. * @ctxt: a regexp parser context
  1393. * @from: the from state
  1394. * @to: the target state or NULL for building a new one
  1395. *
  1396. */
  1397. static void
  1398. xmlFAGenerateEpsilonTransition(xmlRegParserCtxtPtr ctxt,
  1399. xmlRegStatePtr from, xmlRegStatePtr to) {
  1400. if (to == NULL) {
  1401. to = xmlRegNewState(ctxt);
  1402. xmlRegStatePush(ctxt, to);
  1403. ctxt->state = to;
  1404. }
  1405. xmlRegStateAddTrans(ctxt, from, NULL, to, -1, -1);
  1406. }
  1407. /**
  1408. * xmlFAGenerateCountedEpsilonTransition:
  1409. * @ctxt: a regexp parser context
  1410. * @from: the from state
  1411. * @to: the target state or NULL for building a new one
  1412. * counter: the counter for that transition
  1413. *
  1414. */
  1415. static void
  1416. xmlFAGenerateCountedEpsilonTransition(xmlRegParserCtxtPtr ctxt,
  1417. xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
  1418. if (to == NULL) {
  1419. to = xmlRegNewState(ctxt);
  1420. xmlRegStatePush(ctxt, to);
  1421. ctxt->state = to;
  1422. }
  1423. xmlRegStateAddTrans(ctxt, from, NULL, to, counter, -1);
  1424. }
  1425. /**
  1426. * xmlFAGenerateCountedTransition:
  1427. * @ctxt: a regexp parser context
  1428. * @from: the from state
  1429. * @to: the target state or NULL for building a new one
  1430. * counter: the counter for that transition
  1431. *
  1432. */
  1433. static void
  1434. xmlFAGenerateCountedTransition(xmlRegParserCtxtPtr ctxt,
  1435. xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
  1436. if (to == NULL) {
  1437. to = xmlRegNewState(ctxt);
  1438. xmlRegStatePush(ctxt, to);
  1439. ctxt->state = to;
  1440. }
  1441. xmlRegStateAddTrans(ctxt, from, NULL, to, -1, counter);
  1442. }
  1443. /**
  1444. * xmlFAGenerateTransitions:
  1445. * @ctxt: a regexp parser context
  1446. * @from: the from state
  1447. * @to: the target state or NULL for building a new one
  1448. * @atom: the atom generating the transition
  1449. *
  1450. * Returns 0 if success and -1 in case of error.
  1451. */
  1452. static int
  1453. xmlFAGenerateTransitions(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr from,
  1454. xmlRegStatePtr to, xmlRegAtomPtr atom) {
  1455. xmlRegStatePtr end;
  1456. int nullable = 0;
  1457. if (atom == NULL) {
  1458. ERROR("generate transition: atom == NULL");
  1459. return(-1);
  1460. }
  1461. if (atom->type == XML_REGEXP_SUBREG) {
  1462. /*
  1463. * this is a subexpression handling one should not need to
  1464. * create a new node except for XML_REGEXP_QUANT_RANGE.
  1465. */
  1466. if (xmlRegAtomPush(ctxt, atom) < 0) {
  1467. return(-1);
  1468. }
  1469. if ((to != NULL) && (atom->stop != to) &&
  1470. (atom->quant != XML_REGEXP_QUANT_RANGE)) {
  1471. /*
  1472. * Generate an epsilon transition to link to the target
  1473. */
  1474. xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to);
  1475. #ifdef DV
  1476. } else if ((to == NULL) && (atom->quant != XML_REGEXP_QUANT_RANGE) &&
  1477. (atom->quant != XML_REGEXP_QUANT_ONCE)) {
  1478. to = xmlRegNewState(ctxt);
  1479. xmlRegStatePush(ctxt, to);
  1480. ctxt->state = to;
  1481. xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to);
  1482. #endif
  1483. }
  1484. switch (atom->quant) {
  1485. case XML_REGEXP_QUANT_OPT:
  1486. atom->quant = XML_REGEXP_QUANT_ONCE;
  1487. /*
  1488. * transition done to the state after end of atom.
  1489. * 1. set transition from atom start to new state
  1490. * 2. set transition from atom end to this state.
  1491. */
  1492. if (to == NULL) {
  1493. xmlFAGenerateEpsilonTransition(ctxt, atom->start, 0);
  1494. xmlFAGenerateEpsilonTransition(ctxt, atom->stop,
  1495. ctxt->state);
  1496. } else {
  1497. xmlFAGenerateEpsilonTransition(ctxt, atom->start, to);
  1498. }
  1499. break;
  1500. case XML_REGEXP_QUANT_MULT:
  1501. atom->quant = XML_REGEXP_QUANT_ONCE;
  1502. xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
  1503. xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
  1504. break;
  1505. case XML_REGEXP_QUANT_PLUS:
  1506. atom->quant = XML_REGEXP_QUANT_ONCE;
  1507. xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
  1508. break;
  1509. case XML_REGEXP_QUANT_RANGE: {
  1510. int counter;
  1511. xmlRegStatePtr inter, newstate;
  1512. /*
  1513. * create the final state now if needed
  1514. */
  1515. if (to != NULL) {
  1516. newstate = to;
  1517. } else {
  1518. newstate = xmlRegNewState(ctxt);
  1519. xmlRegStatePush(ctxt, newstate);
  1520. }
  1521. /*
  1522. * The principle here is to use counted transition
  1523. * to avoid explosion in the number of states in the
  1524. * graph. This is clearly more complex but should not
  1525. * be exploitable at runtime.
  1526. */
  1527. if ((atom->min == 0) && (atom->start0 == NULL)) {
  1528. xmlRegAtomPtr copy;
  1529. /*
  1530. * duplicate a transition based on atom to count next
  1531. * occurrences after 1. We cannot loop to atom->start
  1532. * directly because we need an epsilon transition to
  1533. * newstate.
  1534. */
  1535. /* ???? For some reason it seems we never reach that
  1536. case, I suppose this got optimized out before when
  1537. building the automata */
  1538. copy = xmlRegCopyAtom(ctxt, atom);
  1539. if (copy == NULL)
  1540. return(-1);
  1541. copy->quant = XML_REGEXP_QUANT_ONCE;
  1542. copy->min = 0;
  1543. copy->max = 0;
  1544. if (xmlFAGenerateTransitions(ctxt, atom->start, NULL, copy)
  1545. < 0)
  1546. return(-1);
  1547. inter = ctxt->state;
  1548. counter = xmlRegGetCounter(ctxt);
  1549. ctxt->counters[counter].min = atom->min - 1;
  1550. ctxt->counters[counter].max = atom->max - 1;
  1551. /* count the number of times we see it again */
  1552. xmlFAGenerateCountedEpsilonTransition(ctxt, inter,
  1553. atom->stop, counter);
  1554. /* allow a way out based on the count */
  1555. xmlFAGenerateCountedTransition(ctxt, inter,
  1556. newstate, counter);
  1557. /* and also allow a direct exit for 0 */
  1558. xmlFAGenerateEpsilonTransition(ctxt, atom->start,
  1559. newstate);
  1560. } else {
  1561. /*
  1562. * either we need the atom at least once or there
  1563. * is an atom->start0 allowing to easily plug the
  1564. * epsilon transition.
  1565. */
  1566. counter = xmlRegGetCounter(ctxt);
  1567. ctxt->counters[counter].min = atom->min - 1;
  1568. ctxt->counters[counter].max = atom->max - 1;
  1569. /* count the number of times we see it again */
  1570. xmlFAGenerateCountedEpsilonTransition(ctxt, atom->stop,
  1571. atom->start, counter);
  1572. /* allow a way out based on the count */
  1573. xmlFAGenerateCountedTransition(ctxt, atom->stop,
  1574. newstate, counter);
  1575. /* and if needed allow a direct exit for 0 */
  1576. if (atom->min == 0)
  1577. xmlFAGenerateEpsilonTransition(ctxt, atom->start0,
  1578. newstate);
  1579. }
  1580. atom->min = 0;
  1581. atom->max = 0;
  1582. atom->quant = XML_REGEXP_QUANT_ONCE;
  1583. ctxt->state = newstate;
  1584. }
  1585. default:
  1586. break;
  1587. }
  1588. return(0);
  1589. }
  1590. if ((atom->min == 0) && (atom->max == 0) &&
  1591. (atom->quant == XML_REGEXP_QUANT_RANGE)) {
  1592. /*
  1593. * we can discard the atom and generate an epsilon transition instead
  1594. */
  1595. if (to == NULL) {
  1596. to = xmlRegNewState(ctxt);
  1597. if (to != NULL)
  1598. xmlRegStatePush(ctxt, to);
  1599. else {
  1600. return(-1);
  1601. }
  1602. }
  1603. xmlFAGenerateEpsilonTransition(ctxt, from, to);
  1604. ctxt->state = to;
  1605. xmlRegFreeAtom(atom);
  1606. return(0);
  1607. }
  1608. if (to == NULL) {
  1609. to = xmlRegNewState(ctxt);
  1610. if (to != NULL)
  1611. xmlRegStatePush(ctxt, to);
  1612. else {
  1613. return(-1);
  1614. }
  1615. }
  1616. end = to;
  1617. if ((atom->quant == XML_REGEXP_QUANT_MULT) ||
  1618. (atom->quant == XML_REGEXP_QUANT_PLUS)) {
  1619. /*
  1620. * Do not pollute the target state by adding transitions from
  1621. * it as it is likely to be the shared target of multiple branches.
  1622. * So isolate with an epsilon transition.
  1623. */
  1624. xmlRegStatePtr tmp;
  1625. tmp = xmlRegNewState(ctxt);
  1626. if (tmp != NULL)
  1627. xmlRegStatePush(ctxt, tmp);
  1628. else {
  1629. return(-1);
  1630. }
  1631. xmlFAGenerateEpsilonTransition(ctxt, tmp, to);
  1632. to = tmp;
  1633. }
  1634. if (xmlRegAtomPush(ctxt, atom) < 0) {
  1635. return(-1);
  1636. }
  1637. if ((atom->quant == XML_REGEXP_QUANT_RANGE) &&
  1638. (atom->min == 0) && (atom->max > 0)) {
  1639. nullable = 1;
  1640. atom->min = 1;
  1641. if (atom->max == 1)
  1642. atom->quant = XML_REGEXP_QUANT_OPT;
  1643. }
  1644. xmlRegStateAddTrans(ctxt, from, atom, to, -1, -1);
  1645. ctxt->state = end;
  1646. switch (atom->quant) {
  1647. case XML_REGEXP_QUANT_OPT:
  1648. atom->quant = XML_REGEXP_QUANT_ONCE;
  1649. xmlFAGenerateEpsilonTransition(ctxt, from, to);
  1650. break;
  1651. case XML_REGEXP_QUANT_MULT:
  1652. atom->quant = XML_REGEXP_QUANT_ONCE;
  1653. xmlFAGenerateEpsilonTransition(ctxt, from, to);
  1654. xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
  1655. break;
  1656. case XML_REGEXP_QUANT_PLUS:
  1657. atom->quant = XML_REGEXP_QUANT_ONCE;
  1658. xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
  1659. break;
  1660. case XML_REGEXP_QUANT_RANGE:
  1661. if (nullable)
  1662. xmlFAGenerateEpsilonTransition(ctxt, from, to);
  1663. break;
  1664. default:
  1665. break;
  1666. }
  1667. return(0);
  1668. }
  1669. /**
  1670. * xmlFAReduceEpsilonTransitions:
  1671. * @ctxt: a regexp parser context
  1672. * @fromnr: the from state
  1673. * @tonr: the to state
  1674. * @counter: should that transition be associated to a counted
  1675. *
  1676. */
  1677. static void
  1678. xmlFAReduceEpsilonTransitions(xmlRegParserCtxtPtr ctxt, int fromnr,
  1679. int tonr, int counter) {
  1680. int transnr;
  1681. xmlRegStatePtr from;
  1682. xmlRegStatePtr to;
  1683. #ifdef DEBUG_REGEXP_GRAPH
  1684. printf("xmlFAReduceEpsilonTransitions(%d, %d)\n", fromnr, tonr);
  1685. #endif
  1686. from = ctxt->states[fromnr];
  1687. if (from == NULL)
  1688. return;
  1689. to = ctxt->states[tonr];
  1690. if (to == NULL)
  1691. return;
  1692. if ((to->mark == XML_REGEXP_MARK_START) ||
  1693. (to->mark == XML_REGEXP_MARK_VISITED))
  1694. return;
  1695. to->mark = XML_REGEXP_MARK_VISITED;
  1696. if (to->type == XML_REGEXP_FINAL_STATE) {
  1697. #ifdef DEBUG_REGEXP_GRAPH
  1698. printf("State %d is final, so %d becomes final\n", tonr, fromnr);
  1699. #endif
  1700. from->type = XML_REGEXP_FINAL_STATE;
  1701. }
  1702. for (transnr = 0;transnr < to->nbTrans;transnr++) {
  1703. if (to->trans[transnr].to < 0)
  1704. continue;
  1705. if (to->trans[transnr].atom == NULL) {
  1706. /*
  1707. * Don't remove counted transitions
  1708. * Don't loop either
  1709. */
  1710. if (to->trans[transnr].to != fromnr) {
  1711. if (to->trans[transnr].count >= 0) {
  1712. int newto = to->trans[transnr].to;
  1713. xmlRegStateAddTrans(ctxt, from, NULL,
  1714. ctxt->states[newto],
  1715. -1, to->trans[transnr].count);
  1716. } else {
  1717. #ifdef DEBUG_REGEXP_GRAPH
  1718. printf("Found epsilon trans %d from %d to %d\n",
  1719. transnr, tonr, to->trans[transnr].to);
  1720. #endif
  1721. if (to->trans[transnr].counter >= 0) {
  1722. xmlFAReduceEpsilonTransitions(ctxt, fromnr,
  1723. to->trans[transnr].to,
  1724. to->trans[transnr].counter);
  1725. } else {
  1726. xmlFAReduceEpsilonTransitions(ctxt, fromnr,
  1727. to->trans[transnr].to,
  1728. counter);
  1729. }
  1730. }
  1731. }
  1732. } else {
  1733. int newto = to->trans[transnr].to;
  1734. if (to->trans[transnr].counter >= 0) {
  1735. xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
  1736. ctxt->states[newto],
  1737. to->trans[transnr].counter, -1);
  1738. } else {
  1739. xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
  1740. ctxt->states[newto], counter, -1);
  1741. }
  1742. }
  1743. }
  1744. to->mark = XML_REGEXP_MARK_NORMAL;
  1745. }
  1746. /**
  1747. * xmlFAEliminateSimpleEpsilonTransitions:
  1748. * @ctxt: a regexp parser context
  1749. *
  1750. * Eliminating general epsilon transitions can get costly in the general
  1751. * algorithm due to the large amount of generated new transitions and
  1752. * associated comparisons. However for simple epsilon transition used just
  1753. * to separate building blocks when generating the automata this can be
  1754. * reduced to state elimination:
  1755. * - if there exists an epsilon from X to Y
  1756. * - if there is no other transition from X
  1757. * then X and Y are semantically equivalent and X can be eliminated
  1758. * If X is the start state then make Y the start state, else replace the
  1759. * target of all transitions to X by transitions to Y.
  1760. *
  1761. * If X is a final state, skip it.
  1762. * Otherwise it would be necessary to manipulate counters for this case when
  1763. * eliminating state 2:
  1764. * State 1 has a transition with an atom to state 2.
  1765. * State 2 is final and has an epsilon transition to state 1.
  1766. */
  1767. static void
  1768. xmlFAEliminateSimpleEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
  1769. int statenr, i, j, newto;
  1770. xmlRegStatePtr state, tmp;
  1771. for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
  1772. state = ctxt->states[statenr];
  1773. if (state == NULL)
  1774. continue;
  1775. if (state->nbTrans != 1)
  1776. continue;
  1777. if (state->type == XML_REGEXP_UNREACH_STATE ||
  1778. state->type == XML_REGEXP_FINAL_STATE)
  1779. continue;
  1780. /* is the only transition out a basic transition */
  1781. if ((state->trans[0].atom == NULL) &&
  1782. (state->trans[0].to >= 0) &&
  1783. (state->trans[0].to != statenr) &&
  1784. (state->trans[0].counter < 0) &&
  1785. (state->trans[0].count < 0)) {
  1786. newto = state->trans[0].to;
  1787. if (state->type == XML_REGEXP_START_STATE) {
  1788. #ifdef DEBUG_REGEXP_GRAPH
  1789. printf("Found simple epsilon trans from start %d to %d\n",
  1790. statenr, newto);
  1791. #endif
  1792. } else {
  1793. #ifdef DEBUG_REGEXP_GRAPH
  1794. printf("Found simple epsilon trans from %d to %d\n",
  1795. statenr, newto);
  1796. #endif
  1797. for (i = 0;i < state->nbTransTo;i++) {
  1798. tmp = ctxt->states[state->transTo[i]];
  1799. for (j = 0;j < tmp->nbTrans;j++) {
  1800. if (tmp->trans[j].to == statenr) {
  1801. #ifdef DEBUG_REGEXP_GRAPH
  1802. printf("Changed transition %d on %d to go to %d\n",
  1803. j, tmp->no, newto);
  1804. #endif
  1805. tmp->trans[j].to = -1;
  1806. xmlRegStateAddTrans(ctxt, tmp, tmp->trans[j].atom,
  1807. ctxt->states[newto],
  1808. tmp->trans[j].counter,
  1809. tmp->trans[j].count);
  1810. }
  1811. }
  1812. }
  1813. if (state->type == XML_REGEXP_FINAL_STATE)
  1814. ctxt->states[newto]->type = XML_REGEXP_FINAL_STATE;
  1815. /* eliminate the transition completely */
  1816. state->nbTrans = 0;
  1817. state->type = XML_REGEXP_UNREACH_STATE;
  1818. }
  1819. }
  1820. }
  1821. }
  1822. /**
  1823. * xmlFAEliminateEpsilonTransitions:
  1824. * @ctxt: a regexp parser context
  1825. *
  1826. */
  1827. static void
  1828. xmlFAEliminateEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
  1829. int statenr, transnr;
  1830. xmlRegStatePtr state;
  1831. int has_epsilon;
  1832. if (ctxt->states == NULL) return;
  1833. /*
  1834. * Eliminate simple epsilon transition and the associated unreachable
  1835. * states.
  1836. */
  1837. xmlFAEliminateSimpleEpsilonTransitions(ctxt);
  1838. for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
  1839. state = ctxt->states[statenr];
  1840. if ((state != NULL) && (state->type == XML_REGEXP_UNREACH_STATE)) {
  1841. #ifdef DEBUG_REGEXP_GRAPH
  1842. printf("Removed unreachable state %d\n", statenr);
  1843. #endif
  1844. xmlRegFreeState(state);
  1845. ctxt->states[statenr] = NULL;
  1846. }
  1847. }
  1848. has_epsilon = 0;
  1849. /*
  1850. * Build the completed transitions bypassing the epsilons
  1851. * Use a marking algorithm to avoid loops
  1852. * Mark sink states too.
  1853. * Process from the latest states backward to the start when
  1854. * there is long cascading epsilon chains this minimize the
  1855. * recursions and transition compares when adding the new ones
  1856. */
  1857. for (statenr = ctxt->nbStates - 1;statenr >= 0;statenr--) {
  1858. state = ctxt->states[statenr];
  1859. if (state == NULL)
  1860. continue;
  1861. if ((state->nbTrans == 0) &&
  1862. (state->type != XML_REGEXP_FINAL_STATE)) {
  1863. state->type = XML_REGEXP_SINK_STATE;
  1864. }
  1865. for (transnr = 0;transnr < state->nbTrans;transnr++) {
  1866. if ((state->trans[transnr].atom == NULL) &&
  1867. (state->trans[transnr].to >= 0)) {
  1868. if (state->trans[transnr].to == statenr) {
  1869. state->trans[transnr].to = -1;
  1870. #ifdef DEBUG_REGEXP_GRAPH
  1871. printf("Removed loopback epsilon trans %d on %d\n",
  1872. transnr, statenr);
  1873. #endif
  1874. } else if (state->trans[transnr].count < 0) {
  1875. int newto = state->trans[transnr].to;
  1876. #ifdef DEBUG_REGEXP_GRAPH
  1877. printf("Found epsilon trans %d from %d to %d\n",
  1878. transnr, statenr, newto);
  1879. #endif
  1880. has_epsilon = 1;
  1881. state->trans[transnr].to = -2;
  1882. state->mark = XML_REGEXP_MARK_START;
  1883. xmlFAReduceEpsilonTransitions(ctxt, statenr,
  1884. newto, state->trans[transnr].counter);
  1885. state->mark = XML_REGEXP_MARK_NORMAL;
  1886. #ifdef DEBUG_REGEXP_GRAPH
  1887. } else {
  1888. printf("Found counted transition %d on %d\n",
  1889. transnr, statenr);
  1890. #endif
  1891. }
  1892. }
  1893. }
  1894. }
  1895. /*
  1896. * Eliminate the epsilon transitions
  1897. */
  1898. if (has_epsilon) {
  1899. for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
  1900. state = ctxt->states[statenr];
  1901. if (state == NULL)
  1902. continue;
  1903. for (transnr = 0;transnr < state->nbTrans;transnr++) {
  1904. xmlRegTransPtr trans = &(state->trans[transnr]);
  1905. if ((trans->atom == NULL) &&
  1906. (trans->count < 0) &&
  1907. (trans->to >= 0)) {
  1908. trans->to = -1;
  1909. }
  1910. }
  1911. }
  1912. }
  1913. /*
  1914. * Use this pass to detect unreachable states too
  1915. */
  1916. for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
  1917. state = ctxt->states[statenr];
  1918. if (state != NULL)
  1919. state->reached = XML_REGEXP_MARK_NORMAL;
  1920. }
  1921. state = ctxt->states[0];
  1922. if (state != NULL)
  1923. state->reached = XML_REGEXP_MARK_START;
  1924. while (state != NULL) {
  1925. xmlRegStatePtr target = NULL;
  1926. state->reached = XML_REGEXP_MARK_VISITED;
  1927. /*
  1928. * Mark all states reachable from the current reachable state
  1929. */
  1930. for (transnr = 0;transnr < state->nbTrans;transnr++) {
  1931. if ((state->trans[transnr].to >= 0) &&
  1932. ((state->trans[transnr].atom != NULL) ||
  1933. (state->trans[transnr].count >= 0))) {
  1934. int newto = state->trans[transnr].to;
  1935. if (ctxt->states[newto] == NULL)
  1936. continue;
  1937. if (ctxt->states[newto]->reached == XML_REGEXP_MARK_NORMAL) {
  1938. ctxt->states[newto]->reached = XML_REGEXP_MARK_START;
  1939. target = ctxt->states[newto];
  1940. }
  1941. }
  1942. }
  1943. /*
  1944. * find the next accessible state not explored
  1945. */
  1946. if (target == NULL) {
  1947. for (statenr = 1;statenr < ctxt->nbStates;statenr++) {
  1948. state = ctxt->states[statenr];
  1949. if ((state != NULL) && (state->reached ==
  1950. XML_REGEXP_MARK_START)) {
  1951. target = state;
  1952. break;
  1953. }
  1954. }
  1955. }
  1956. state = target;
  1957. }
  1958. for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
  1959. state = ctxt->states[statenr];
  1960. if ((state != NULL) && (state->reached == XML_REGEXP_MARK_NORMAL)) {
  1961. #ifdef DEBUG_REGEXP_GRAPH
  1962. printf("Removed unreachable state %d\n", statenr);
  1963. #endif
  1964. xmlRegFreeState(state);
  1965. ctxt->states[statenr] = NULL;
  1966. }
  1967. }
  1968. }
  1969. static int
  1970. xmlFACompareRanges(xmlRegRangePtr range1, xmlRegRangePtr range2) {
  1971. int ret = 0;
  1972. if ((range1->type == XML_REGEXP_RANGES) ||
  1973. (range2->type == XML_REGEXP_RANGES) ||
  1974. (range2->type == XML_REGEXP_SUBREG) ||
  1975. (range1->type == XML_REGEXP_SUBREG) ||
  1976. (range1->type == XML_REGEXP_STRING) ||
  1977. (range2->type == XML_REGEXP_STRING))
  1978. return(-1);
  1979. /* put them in order */
  1980. if (range1->type > range2->type) {
  1981. xmlRegRangePtr tmp;
  1982. tmp = range1;
  1983. range1 = range2;
  1984. range2 = tmp;
  1985. }
  1986. if ((range1->type == XML_REGEXP_ANYCHAR) ||
  1987. (range2->type == XML_REGEXP_ANYCHAR)) {
  1988. ret = 1;
  1989. } else if ((range1->type == XML_REGEXP_EPSILON) ||
  1990. (range2->type == XML_REGEXP_EPSILON)) {
  1991. return(0);
  1992. } else if (range1->type == range2->type) {
  1993. if (range1->type != XML_REGEXP_CHARVAL)
  1994. ret = 1;
  1995. else if ((range1->end < range2->start) ||
  1996. (range2->end < range1->start))
  1997. ret = 0;
  1998. else
  1999. ret = 1;
  2000. } else if (range1->type == XML_REGEXP_CHARVAL) {
  2001. int codepoint;
  2002. int neg = 0;
  2003. /*
  2004. * just check all codepoints in the range for acceptance,
  2005. * this is usually way cheaper since done only once at
  2006. * compilation than testing over and over at runtime or
  2007. * pushing too many states when evaluating.
  2008. */
  2009. if (((range1->neg == 0) && (range2->neg != 0)) ||
  2010. ((range1->neg != 0) && (range2->neg == 0)))
  2011. neg = 1;
  2012. for (codepoint = range1->start;codepoint <= range1->end ;codepoint++) {
  2013. ret = xmlRegCheckCharacterRange(range2->type, codepoint,
  2014. 0, range2->start, range2->end,
  2015. range2->blockName);
  2016. if (ret < 0)
  2017. return(-1);
  2018. if (((neg == 1) && (ret == 0)) ||
  2019. ((neg == 0) && (ret == 1)))
  2020. return(1);
  2021. }
  2022. return(0);
  2023. } else if ((range1->type == XML_REGEXP_BLOCK_NAME) ||
  2024. (range2->type == XML_REGEXP_BLOCK_NAME)) {
  2025. if (range1->type == range2->type) {
  2026. ret = xmlStrEqual(range1->blockName, range2->blockName);
  2027. } else {
  2028. /*
  2029. * comparing a block range with anything else is way
  2030. * too costly, and maintaining the table is like too much
  2031. * memory too, so let's force the automata to save state
  2032. * here.
  2033. */
  2034. return(1);
  2035. }
  2036. } else if ((range1->type < XML_REGEXP_LETTER) ||
  2037. (range2->type < XML_REGEXP_LETTER)) {
  2038. if ((range1->type == XML_REGEXP_ANYSPACE) &&
  2039. (range2->type == XML_REGEXP_NOTSPACE))
  2040. ret = 0;
  2041. else if ((range1->type == XML_REGEXP_INITNAME) &&
  2042. (range2->type == XML_REGEXP_NOTINITNAME))
  2043. ret = 0;
  2044. else if ((range1->type == XML_REGEXP_NAMECHAR) &&
  2045. (range2->type == XML_REGEXP_NOTNAMECHAR))
  2046. ret = 0;
  2047. else if ((range1->type == XML_REGEXP_DECIMAL) &&
  2048. (range2->type == XML_REGEXP_NOTDECIMAL))
  2049. ret = 0;
  2050. else if ((range1->type == XML_REGEXP_REALCHAR) &&
  2051. (range2->type == XML_REGEXP_NOTREALCHAR))
  2052. ret = 0;
  2053. else {
  2054. /* same thing to limit complexity */
  2055. return(1);
  2056. }
  2057. } else {
  2058. ret = 0;
  2059. /* range1->type < range2->type here */
  2060. switch (range1->type) {
  2061. case XML_REGEXP_LETTER:
  2062. /* all disjoint except in the subgroups */
  2063. if ((range2->type == XML_REGEXP_LETTER_UPPERCASE) ||
  2064. (range2->type == XML_REGEXP_LETTER_LOWERCASE) ||
  2065. (range2->type == XML_REGEXP_LETTER_TITLECASE) ||
  2066. (range2->type == XML_REGEXP_LETTER_MODIFIER) ||
  2067. (range2->type == XML_REGEXP_LETTER_OTHERS))
  2068. ret = 1;
  2069. break;
  2070. case XML_REGEXP_MARK:
  2071. if ((range2->type == XML_REGEXP_MARK_NONSPACING) ||
  2072. (range2->type == XML_REGEXP_MARK_SPACECOMBINING) ||
  2073. (range2->type == XML_REGEXP_MARK_ENCLOSING))
  2074. ret = 1;
  2075. break;
  2076. case XML_REGEXP_NUMBER:
  2077. if ((range2->type == XML_REGEXP_NUMBER_DECIMAL) ||
  2078. (range2->type == XML_REGEXP_NUMBER_LETTER) ||
  2079. (range2->type == XML_REGEXP_NUMBER_OTHERS))
  2080. ret = 1;
  2081. break;
  2082. case XML_REGEXP_PUNCT:
  2083. if ((range2->type == XML_REGEXP_PUNCT_CONNECTOR) ||
  2084. (range2->type == XML_REGEXP_PUNCT_DASH) ||
  2085. (range2->type == XML_REGEXP_PUNCT_OPEN) ||
  2086. (range2->type == XML_REGEXP_PUNCT_CLOSE) ||
  2087. (range2->type == XML_REGEXP_PUNCT_INITQUOTE) ||
  2088. (range2->type == XML_REGEXP_PUNCT_FINQUOTE) ||
  2089. (range2->type == XML_REGEXP_PUNCT_OTHERS))
  2090. ret = 1;
  2091. break;
  2092. case XML_REGEXP_SEPAR:
  2093. if ((range2->type == XML_REGEXP_SEPAR_SPACE) ||
  2094. (range2->type == XML_REGEXP_SEPAR_LINE) ||
  2095. (range2->type == XML_REGEXP_SEPAR_PARA))
  2096. ret = 1;
  2097. break;
  2098. case XML_REGEXP_SYMBOL:
  2099. if ((range2->type == XML_REGEXP_SYMBOL_MATH) ||
  2100. (range2->type == XML_REGEXP_SYMBOL_CURRENCY) ||
  2101. (range2->type == XML_REGEXP_SYMBOL_MODIFIER) ||
  2102. (range2->type == XML_REGEXP_SYMBOL_OTHERS))
  2103. ret = 1;
  2104. break;
  2105. case XML_REGEXP_OTHER:
  2106. if ((range2->type == XML_REGEXP_OTHER_CONTROL) ||
  2107. (range2->type == XML_REGEXP_OTHER_FORMAT) ||
  2108. (range2->type == XML_REGEXP_OTHER_PRIVATE))
  2109. ret = 1;
  2110. break;
  2111. default:
  2112. if ((range2->type >= XML_REGEXP_LETTER) &&
  2113. (range2->type < XML_REGEXP_BLOCK_NAME))
  2114. ret = 0;
  2115. else {
  2116. /* safety net ! */
  2117. return(1);
  2118. }
  2119. }
  2120. }
  2121. if (((range1->neg == 0) && (range2->neg != 0)) ||
  2122. ((range1->neg != 0) && (range2->neg == 0)))
  2123. ret = !ret;
  2124. return(ret);
  2125. }
  2126. /**
  2127. * xmlFACompareAtomTypes:
  2128. * @type1: an atom type
  2129. * @type2: an atom type
  2130. *
  2131. * Compares two atoms type to check whether they intersect in some ways,
  2132. * this is used by xmlFACompareAtoms only
  2133. *
  2134. * Returns 1 if they may intersect and 0 otherwise
  2135. */
  2136. static int
  2137. xmlFACompareAtomTypes(xmlRegAtomType type1, xmlRegAtomType type2) {
  2138. if ((type1 == XML_REGEXP_EPSILON) ||
  2139. (type1 == XML_REGEXP_CHARVAL) ||
  2140. (type1 == XML_REGEXP_RANGES) ||
  2141. (type1 == XML_REGEXP_SUBREG) ||
  2142. (type1 == XML_REGEXP_STRING) ||
  2143. (type1 == XML_REGEXP_ANYCHAR))
  2144. return(1);
  2145. if ((type2 == XML_REGEXP_EPSILON) ||
  2146. (type2 == XML_REGEXP_CHARVAL) ||
  2147. (type2 == XML_REGEXP_RANGES) ||
  2148. (type2 == XML_REGEXP_SUBREG) ||
  2149. (type2 == XML_REGEXP_STRING) ||
  2150. (type2 == XML_REGEXP_ANYCHAR))
  2151. return(1);
  2152. if (type1 == type2) return(1);
  2153. /* simplify subsequent compares by making sure type1 < type2 */
  2154. if (type1 > type2) {
  2155. xmlRegAtomType tmp = type1;
  2156. type1 = type2;
  2157. type2 = tmp;
  2158. }
  2159. switch (type1) {
  2160. case XML_REGEXP_ANYSPACE: /* \s */
  2161. /* can't be a letter, number, mark, punctuation, symbol */
  2162. if ((type2 == XML_REGEXP_NOTSPACE) ||
  2163. ((type2 >= XML_REGEXP_LETTER) &&
  2164. (type2 <= XML_REGEXP_LETTER_OTHERS)) ||
  2165. ((type2 >= XML_REGEXP_NUMBER) &&
  2166. (type2 <= XML_REGEXP_NUMBER_OTHERS)) ||
  2167. ((type2 >= XML_REGEXP_MARK) &&
  2168. (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
  2169. ((type2 >= XML_REGEXP_PUNCT) &&
  2170. (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
  2171. ((type2 >= XML_REGEXP_SYMBOL) &&
  2172. (type2 <= XML_REGEXP_SYMBOL_OTHERS))
  2173. ) return(0);
  2174. break;
  2175. case XML_REGEXP_NOTSPACE: /* \S */
  2176. break;
  2177. case XML_REGEXP_INITNAME: /* \l */
  2178. /* can't be a number, mark, separator, punctuation, symbol or other */
  2179. if ((type2 == XML_REGEXP_NOTINITNAME) ||
  2180. ((type2 >= XML_REGEXP_NUMBER) &&
  2181. (type2 <= XML_REGEXP_NUMBER_OTHERS)) ||
  2182. ((type2 >= XML_REGEXP_MARK) &&
  2183. (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
  2184. ((type2 >= XML_REGEXP_SEPAR) &&
  2185. (type2 <= XML_REGEXP_SEPAR_PARA)) ||
  2186. ((type2 >= XML_REGEXP_PUNCT) &&
  2187. (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
  2188. ((type2 >= XML_REGEXP_SYMBOL) &&
  2189. (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
  2190. ((type2 >= XML_REGEXP_OTHER) &&
  2191. (type2 <= XML_REGEXP_OTHER_NA))
  2192. ) return(0);
  2193. break;
  2194. case XML_REGEXP_NOTINITNAME: /* \L */
  2195. break;
  2196. case XML_REGEXP_NAMECHAR: /* \c */
  2197. /* can't be a mark, separator, punctuation, symbol or other */
  2198. if ((type2 == XML_REGEXP_NOTNAMECHAR) ||
  2199. ((type2 >= XML_REGEXP_MARK) &&
  2200. (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
  2201. ((type2 >= XML_REGEXP_PUNCT) &&
  2202. (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
  2203. ((type2 >= XML_REGEXP_SEPAR) &&
  2204. (type2 <= XML_REGEXP_SEPAR_PARA)) ||
  2205. ((type2 >= XML_REGEXP_SYMBOL) &&
  2206. (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
  2207. ((type2 >= XML_REGEXP_OTHER) &&
  2208. (type2 <= XML_REGEXP_OTHER_NA))
  2209. ) return(0);
  2210. break;
  2211. case XML_REGEXP_NOTNAMECHAR: /* \C */
  2212. break;
  2213. case XML_REGEXP_DECIMAL: /* \d */
  2214. /* can't be a letter, mark, separator, punctuation, symbol or other */
  2215. if ((type2 == XML_REGEXP_NOTDECIMAL) ||
  2216. (type2 == XML_REGEXP_REALCHAR) ||
  2217. ((type2 >= XML_REGEXP_LETTER) &&
  2218. (type2 <= XML_REGEXP_LETTER_OTHERS)) ||
  2219. ((type2 >= XML_REGEXP_MARK) &&
  2220. (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
  2221. ((type2 >= XML_REGEXP_PUNCT) &&
  2222. (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
  2223. ((type2 >= XML_REGEXP_SEPAR) &&
  2224. (type2 <= XML_REGEXP_SEPAR_PARA)) ||
  2225. ((type2 >= XML_REGEXP_SYMBOL) &&
  2226. (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
  2227. ((type2 >= XML_REGEXP_OTHER) &&
  2228. (type2 <= XML_REGEXP_OTHER_NA))
  2229. )return(0);
  2230. break;
  2231. case XML_REGEXP_NOTDECIMAL: /* \D */
  2232. break;
  2233. case XML_REGEXP_REALCHAR: /* \w */
  2234. /* can't be a mark, separator, punctuation, symbol or other */
  2235. if ((type2 == XML_REGEXP_NOTDECIMAL) ||
  2236. ((type2 >= XML_REGEXP_MARK) &&
  2237. (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
  2238. ((type2 >= XML_REGEXP_PUNCT) &&
  2239. (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
  2240. ((type2 >= XML_REGEXP_SEPAR) &&
  2241. (type2 <= XML_REGEXP_SEPAR_PARA)) ||
  2242. ((type2 >= XML_REGEXP_SYMBOL) &&
  2243. (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
  2244. ((type2 >= XML_REGEXP_OTHER) &&
  2245. (type2 <= XML_REGEXP_OTHER_NA))
  2246. )return(0);
  2247. break;
  2248. case XML_REGEXP_NOTREALCHAR: /* \W */
  2249. break;
  2250. /*
  2251. * at that point we know both type 1 and type2 are from
  2252. * character categories are ordered and are different,
  2253. * it becomes simple because this is a partition
  2254. */
  2255. case XML_REGEXP_LETTER:
  2256. if (type2 <= XML_REGEXP_LETTER_OTHERS)
  2257. return(1);
  2258. return(0);
  2259. case XML_REGEXP_LETTER_UPPERCASE:
  2260. case XML_REGEXP_LETTER_LOWERCASE:
  2261. case XML_REGEXP_LETTER_TITLECASE:
  2262. case XML_REGEXP_LETTER_MODIFIER:
  2263. case XML_REGEXP_LETTER_OTHERS:
  2264. return(0);
  2265. case XML_REGEXP_MARK:
  2266. if (type2 <= XML_REGEXP_MARK_ENCLOSING)
  2267. return(1);
  2268. return(0);
  2269. case XML_REGEXP_MARK_NONSPACING:
  2270. case XML_REGEXP_MARK_SPACECOMBINING:
  2271. case XML_REGEXP_MARK_ENCLOSING:
  2272. return(0);
  2273. case XML_REGEXP_NUMBER:
  2274. if (type2 <= XML_REGEXP_NUMBER_OTHERS)
  2275. return(1);
  2276. return(0);
  2277. case XML_REGEXP_NUMBER_DECIMAL:
  2278. case XML_REGEXP_NUMBER_LETTER:
  2279. case XML_REGEXP_NUMBER_OTHERS:
  2280. return(0);
  2281. case XML_REGEXP_PUNCT:
  2282. if (type2 <= XML_REGEXP_PUNCT_OTHERS)
  2283. return(1);
  2284. return(0);
  2285. case XML_REGEXP_PUNCT_CONNECTOR:
  2286. case XML_REGEXP_PUNCT_DASH:
  2287. case XML_REGEXP_PUNCT_OPEN:
  2288. case XML_REGEXP_PUNCT_CLOSE:
  2289. case XML_REGEXP_PUNCT_INITQUOTE:
  2290. case XML_REGEXP_PUNCT_FINQUOTE:
  2291. case XML_REGEXP_PUNCT_OTHERS:
  2292. return(0);
  2293. case XML_REGEXP_SEPAR:
  2294. if (type2 <= XML_REGEXP_SEPAR_PARA)
  2295. return(1);
  2296. return(0);
  2297. case XML_REGEXP_SEPAR_SPACE:
  2298. case XML_REGEXP_SEPAR_LINE:
  2299. case XML_REGEXP_SEPAR_PARA:
  2300. return(0);
  2301. case XML_REGEXP_SYMBOL:
  2302. if (type2 <= XML_REGEXP_SYMBOL_OTHERS)
  2303. return(1);
  2304. return(0);
  2305. case XML_REGEXP_SYMBOL_MATH:
  2306. case XML_REGEXP_SYMBOL_CURRENCY:
  2307. case XML_REGEXP_SYMBOL_MODIFIER:
  2308. case XML_REGEXP_SYMBOL_OTHERS:
  2309. return(0);
  2310. case XML_REGEXP_OTHER:
  2311. if (type2 <= XML_REGEXP_OTHER_NA)
  2312. return(1);
  2313. return(0);
  2314. case XML_REGEXP_OTHER_CONTROL:
  2315. case XML_REGEXP_OTHER_FORMAT:
  2316. case XML_REGEXP_OTHER_PRIVATE:
  2317. case XML_REGEXP_OTHER_NA:
  2318. return(0);
  2319. default:
  2320. break;
  2321. }
  2322. return(1);
  2323. }
  2324. /**
  2325. * xmlFAEqualAtoms:
  2326. * @atom1: an atom
  2327. * @atom2: an atom
  2328. * @deep: if not set only compare string pointers
  2329. *
  2330. * Compares two atoms to check whether they are the same exactly
  2331. * this is used to remove equivalent transitions
  2332. *
  2333. * Returns 1 if same and 0 otherwise
  2334. */
  2335. static int
  2336. xmlFAEqualAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2, int deep) {
  2337. int ret = 0;
  2338. if (atom1 == atom2)
  2339. return(1);
  2340. if ((atom1 == NULL) || (atom2 == NULL))
  2341. return(0);
  2342. if (atom1->type != atom2->type)
  2343. return(0);
  2344. switch (atom1->type) {
  2345. case XML_REGEXP_EPSILON:
  2346. ret = 0;
  2347. break;
  2348. case XML_REGEXP_STRING:
  2349. if (!deep)
  2350. ret = (atom1->valuep == atom2->valuep);
  2351. else
  2352. ret = xmlStrEqual((xmlChar *)atom1->valuep,
  2353. (xmlChar *)atom2->valuep);
  2354. break;
  2355. case XML_REGEXP_CHARVAL:
  2356. ret = (atom1->codepoint == atom2->codepoint);
  2357. break;
  2358. case XML_REGEXP_RANGES:
  2359. /* too hard to do in the general case */
  2360. ret = 0;
  2361. default:
  2362. break;
  2363. }
  2364. return(ret);
  2365. }
  2366. /**
  2367. * xmlFACompareAtoms:
  2368. * @atom1: an atom
  2369. * @atom2: an atom
  2370. * @deep: if not set only compare string pointers
  2371. *
  2372. * Compares two atoms to check whether they intersect in some ways,
  2373. * this is used by xmlFAComputesDeterminism and xmlFARecurseDeterminism only
  2374. *
  2375. * Returns 1 if yes and 0 otherwise
  2376. */
  2377. static int
  2378. xmlFACompareAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2, int deep) {
  2379. int ret = 1;
  2380. if (atom1 == atom2)
  2381. return(1);
  2382. if ((atom1 == NULL) || (atom2 == NULL))
  2383. return(0);
  2384. if ((atom1->type == XML_REGEXP_ANYCHAR) ||
  2385. (atom2->type == XML_REGEXP_ANYCHAR))
  2386. return(1);
  2387. if (atom1->type > atom2->type) {
  2388. xmlRegAtomPtr tmp;
  2389. tmp = atom1;
  2390. atom1 = atom2;
  2391. atom2 = tmp;
  2392. }
  2393. if (atom1->type != atom2->type) {
  2394. ret = xmlFACompareAtomTypes(atom1->type, atom2->type);
  2395. /* if they can't intersect at the type level break now */
  2396. if (ret == 0)
  2397. return(0);
  2398. }
  2399. switch (atom1->type) {
  2400. case XML_REGEXP_STRING:
  2401. if (!deep)
  2402. ret = (atom1->valuep != atom2->valuep);
  2403. else {
  2404. xmlChar *val1 = (xmlChar *)atom1->valuep;
  2405. xmlChar *val2 = (xmlChar *)atom2->valuep;
  2406. int compound1 = (xmlStrchr(val1, '|') != NULL);
  2407. int compound2 = (xmlStrchr(val2, '|') != NULL);
  2408. /* Ignore negative match flag for ##other namespaces */
  2409. if (compound1 != compound2)
  2410. return(0);
  2411. ret = xmlRegStrEqualWildcard(val1, val2);
  2412. }
  2413. break;
  2414. case XML_REGEXP_EPSILON:
  2415. goto not_determinist;
  2416. case XML_REGEXP_CHARVAL:
  2417. if (atom2->type == XML_REGEXP_CHARVAL) {
  2418. ret = (atom1->codepoint == atom2->codepoint);
  2419. } else {
  2420. ret = xmlRegCheckCharacter(atom2, atom1->codepoint);
  2421. if (ret < 0)
  2422. ret = 1;
  2423. }
  2424. break;
  2425. case XML_REGEXP_RANGES:
  2426. if (atom2->type == XML_REGEXP_RANGES) {
  2427. int i, j, res;
  2428. xmlRegRangePtr r1, r2;
  2429. /*
  2430. * need to check that none of the ranges eventually matches
  2431. */
  2432. for (i = 0;i < atom1->nbRanges;i++) {
  2433. for (j = 0;j < atom2->nbRanges;j++) {
  2434. r1 = atom1->ranges[i];
  2435. r2 = atom2->ranges[j];
  2436. res = xmlFACompareRanges(r1, r2);
  2437. if (res == 1) {
  2438. ret = 1;
  2439. goto done;
  2440. }
  2441. }
  2442. }
  2443. ret = 0;
  2444. }
  2445. break;
  2446. default:
  2447. goto not_determinist;
  2448. }
  2449. done:
  2450. if (atom1->neg != atom2->neg) {
  2451. ret = !ret;
  2452. }
  2453. if (ret == 0)
  2454. return(0);
  2455. not_determinist:
  2456. return(1);
  2457. }
  2458. /**
  2459. * xmlFARecurseDeterminism:
  2460. * @ctxt: a regexp parser context
  2461. *
  2462. * Check whether the associated regexp is determinist,
  2463. * should be called after xmlFAEliminateEpsilonTransitions()
  2464. *
  2465. */
  2466. static int
  2467. xmlFARecurseDeterminism(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
  2468. int to, xmlRegAtomPtr atom) {
  2469. int ret = 1;
  2470. int res;
  2471. int transnr, nbTrans;
  2472. xmlRegTransPtr t1;
  2473. int deep = 1;
  2474. if (state == NULL)
  2475. return(ret);
  2476. if (state->markd == XML_REGEXP_MARK_VISITED)
  2477. return(ret);
  2478. if (ctxt->flags & AM_AUTOMATA_RNG)
  2479. deep = 0;
  2480. /*
  2481. * don't recurse on transitions potentially added in the course of
  2482. * the elimination.
  2483. */
  2484. nbTrans = state->nbTrans;
  2485. for (transnr = 0;transnr < nbTrans;transnr++) {
  2486. t1 = &(state->trans[transnr]);
  2487. /*
  2488. * check transitions conflicting with the one looked at
  2489. */
  2490. if (t1->atom == NULL) {
  2491. if (t1->to < 0)
  2492. continue;
  2493. state->markd = XML_REGEXP_MARK_VISITED;
  2494. res = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
  2495. to, atom);
  2496. if (res == 0) {
  2497. ret = 0;
  2498. /* t1->nd = 1; */
  2499. }
  2500. continue;
  2501. }
  2502. if (t1->to != to)
  2503. continue;
  2504. if (xmlFACompareAtoms(t1->atom, atom, deep)) {
  2505. ret = 0;
  2506. /* mark the transition as non-deterministic */
  2507. t1->nd = 1;
  2508. }
  2509. }
  2510. return(ret);
  2511. }
  2512. /**
  2513. * xmlFAFinishRecurseDeterminism:
  2514. * @ctxt: a regexp parser context
  2515. *
  2516. * Reset flags after checking determinism.
  2517. */
  2518. static void
  2519. xmlFAFinishRecurseDeterminism(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state) {
  2520. int transnr, nbTrans;
  2521. if (state == NULL)
  2522. return;
  2523. if (state->markd != XML_REGEXP_MARK_VISITED)
  2524. return;
  2525. state->markd = 0;
  2526. nbTrans = state->nbTrans;
  2527. for (transnr = 0; transnr < nbTrans; transnr++) {
  2528. xmlRegTransPtr t1 = &state->trans[transnr];
  2529. if ((t1->atom == NULL) && (t1->to >= 0))
  2530. xmlFAFinishRecurseDeterminism(ctxt, ctxt->states[t1->to]);
  2531. }
  2532. }
  2533. /**
  2534. * xmlFAComputesDeterminism:
  2535. * @ctxt: a regexp parser context
  2536. *
  2537. * Check whether the associated regexp is determinist,
  2538. * should be called after xmlFAEliminateEpsilonTransitions()
  2539. *
  2540. */
  2541. static int
  2542. xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt) {
  2543. int statenr, transnr;
  2544. xmlRegStatePtr state;
  2545. xmlRegTransPtr t1, t2, last;
  2546. int i;
  2547. int ret = 1;
  2548. int deep = 1;
  2549. #ifdef DEBUG_REGEXP_GRAPH
  2550. printf("xmlFAComputesDeterminism\n");
  2551. xmlRegPrintCtxt(stdout, ctxt);
  2552. #endif
  2553. if (ctxt->determinist != -1)
  2554. return(ctxt->determinist);
  2555. if (ctxt->flags & AM_AUTOMATA_RNG)
  2556. deep = 0;
  2557. /*
  2558. * First cleanup the automata removing cancelled transitions
  2559. */
  2560. for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
  2561. state = ctxt->states[statenr];
  2562. if (state == NULL)
  2563. continue;
  2564. if (state->nbTrans < 2)
  2565. continue;
  2566. for (transnr = 0;transnr < state->nbTrans;transnr++) {
  2567. t1 = &(state->trans[transnr]);
  2568. /*
  2569. * Determinism checks in case of counted or all transitions
  2570. * will have to be handled separately
  2571. */
  2572. if (t1->atom == NULL) {
  2573. /* t1->nd = 1; */
  2574. continue;
  2575. }
  2576. if (t1->to == -1) /* eliminated */
  2577. continue;
  2578. for (i = 0;i < transnr;i++) {
  2579. t2 = &(state->trans[i]);
  2580. if (t2->to == -1) /* eliminated */
  2581. continue;
  2582. if (t2->atom != NULL) {
  2583. if (t1->to == t2->to) {
  2584. /*
  2585. * Here we use deep because we want to keep the
  2586. * transitions which indicate a conflict
  2587. */
  2588. if (xmlFAEqualAtoms(t1->atom, t2->atom, deep) &&
  2589. (t1->counter == t2->counter) &&
  2590. (t1->count == t2->count))
  2591. t2->to = -1; /* eliminated */
  2592. }
  2593. }
  2594. }
  2595. }
  2596. }
  2597. /*
  2598. * Check for all states that there aren't 2 transitions
  2599. * with the same atom and a different target.
  2600. */
  2601. for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
  2602. state = ctxt->states[statenr];
  2603. if (state == NULL)
  2604. continue;
  2605. if (state->nbTrans < 2)
  2606. continue;
  2607. last = NULL;
  2608. for (transnr = 0;transnr < state->nbTrans;transnr++) {
  2609. t1 = &(state->trans[transnr]);
  2610. /*
  2611. * Determinism checks in case of counted or all transitions
  2612. * will have to be handled separately
  2613. */
  2614. if (t1->atom == NULL) {
  2615. continue;
  2616. }
  2617. if (t1->to == -1) /* eliminated */
  2618. continue;
  2619. for (i = 0;i < transnr;i++) {
  2620. t2 = &(state->trans[i]);
  2621. if (t2->to == -1) /* eliminated */
  2622. continue;
  2623. if (t2->atom != NULL) {
  2624. /*
  2625. * But here we don't use deep because we want to
  2626. * find transitions which indicate a conflict
  2627. */
  2628. if (xmlFACompareAtoms(t1->atom, t2->atom, 1)) {
  2629. ret = 0;
  2630. /* mark the transitions as non-deterministic ones */
  2631. t1->nd = 1;
  2632. t2->nd = 1;
  2633. last = t1;
  2634. }
  2635. } else if (t1->to != -1) {
  2636. /*
  2637. * do the closure in case of remaining specific
  2638. * epsilon transitions like choices or all
  2639. */
  2640. ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
  2641. t2->to, t2->atom);
  2642. xmlFAFinishRecurseDeterminism(ctxt, ctxt->states[t1->to]);
  2643. /* don't shortcut the computation so all non deterministic
  2644. transition get marked down
  2645. if (ret == 0)
  2646. return(0);
  2647. */
  2648. if (ret == 0) {
  2649. t1->nd = 1;
  2650. /* t2->nd = 1; */
  2651. last = t1;
  2652. }
  2653. }
  2654. }
  2655. /* don't shortcut the computation so all non deterministic
  2656. transition get marked down
  2657. if (ret == 0)
  2658. break; */
  2659. }
  2660. /*
  2661. * mark specifically the last non-deterministic transition
  2662. * from a state since there is no need to set-up rollback
  2663. * from it
  2664. */
  2665. if (last != NULL) {
  2666. last->nd = 2;
  2667. }
  2668. /* don't shortcut the computation so all non deterministic
  2669. transition get marked down
  2670. if (ret == 0)
  2671. break; */
  2672. }
  2673. ctxt->determinist = ret;
  2674. return(ret);
  2675. }
  2676. /************************************************************************
  2677. * *
  2678. * Routines to check input against transition atoms *
  2679. * *
  2680. ************************************************************************/
  2681. static int
  2682. xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint, int neg,
  2683. int start, int end, const xmlChar *blockName) {
  2684. int ret = 0;
  2685. switch (type) {
  2686. case XML_REGEXP_STRING:
  2687. case XML_REGEXP_SUBREG:
  2688. case XML_REGEXP_RANGES:
  2689. case XML_REGEXP_EPSILON:
  2690. return(-1);
  2691. case XML_REGEXP_ANYCHAR:
  2692. ret = ((codepoint != '\n') && (codepoint != '\r'));
  2693. break;
  2694. case XML_REGEXP_CHARVAL:
  2695. ret = ((codepoint >= start) && (codepoint <= end));
  2696. break;
  2697. case XML_REGEXP_NOTSPACE:
  2698. neg = !neg;
  2699. /* Falls through. */
  2700. case XML_REGEXP_ANYSPACE:
  2701. ret = ((codepoint == '\n') || (codepoint == '\r') ||
  2702. (codepoint == '\t') || (codepoint == ' '));
  2703. break;
  2704. case XML_REGEXP_NOTINITNAME:
  2705. neg = !neg;
  2706. /* Falls through. */
  2707. case XML_REGEXP_INITNAME:
  2708. ret = (IS_LETTER(codepoint) ||
  2709. (codepoint == '_') || (codepoint == ':'));
  2710. break;
  2711. case XML_REGEXP_NOTNAMECHAR:
  2712. neg = !neg;
  2713. /* Falls through. */
  2714. case XML_REGEXP_NAMECHAR:
  2715. ret = (IS_LETTER(codepoint) || IS_DIGIT(codepoint) ||
  2716. (codepoint == '.') || (codepoint == '-') ||
  2717. (codepoint == '_') || (codepoint == ':') ||
  2718. IS_COMBINING(codepoint) || IS_EXTENDER(codepoint));
  2719. break;
  2720. case XML_REGEXP_NOTDECIMAL:
  2721. neg = !neg;
  2722. /* Falls through. */
  2723. case XML_REGEXP_DECIMAL:
  2724. ret = xmlUCSIsCatNd(codepoint);
  2725. break;
  2726. case XML_REGEXP_REALCHAR:
  2727. neg = !neg;
  2728. /* Falls through. */
  2729. case XML_REGEXP_NOTREALCHAR:
  2730. ret = xmlUCSIsCatP(codepoint);
  2731. if (ret == 0)
  2732. ret = xmlUCSIsCatZ(codepoint);
  2733. if (ret == 0)
  2734. ret = xmlUCSIsCatC(codepoint);
  2735. break;
  2736. case XML_REGEXP_LETTER:
  2737. ret = xmlUCSIsCatL(codepoint);
  2738. break;
  2739. case XML_REGEXP_LETTER_UPPERCASE:
  2740. ret = xmlUCSIsCatLu(codepoint);
  2741. break;
  2742. case XML_REGEXP_LETTER_LOWERCASE:
  2743. ret = xmlUCSIsCatLl(codepoint);
  2744. break;
  2745. case XML_REGEXP_LETTER_TITLECASE:
  2746. ret = xmlUCSIsCatLt(codepoint);
  2747. break;
  2748. case XML_REGEXP_LETTER_MODIFIER:
  2749. ret = xmlUCSIsCatLm(codepoint);
  2750. break;
  2751. case XML_REGEXP_LETTER_OTHERS:
  2752. ret = xmlUCSIsCatLo(codepoint);
  2753. break;
  2754. case XML_REGEXP_MARK:
  2755. ret = xmlUCSIsCatM(codepoint);
  2756. break;
  2757. case XML_REGEXP_MARK_NONSPACING:
  2758. ret = xmlUCSIsCatMn(codepoint);
  2759. break;
  2760. case XML_REGEXP_MARK_SPACECOMBINING:
  2761. ret = xmlUCSIsCatMc(codepoint);
  2762. break;
  2763. case XML_REGEXP_MARK_ENCLOSING:
  2764. ret = xmlUCSIsCatMe(codepoint);
  2765. break;
  2766. case XML_REGEXP_NUMBER:
  2767. ret = xmlUCSIsCatN(codepoint);
  2768. break;
  2769. case XML_REGEXP_NUMBER_DECIMAL:
  2770. ret = xmlUCSIsCatNd(codepoint);
  2771. break;
  2772. case XML_REGEXP_NUMBER_LETTER:
  2773. ret = xmlUCSIsCatNl(codepoint);
  2774. break;
  2775. case XML_REGEXP_NUMBER_OTHERS:
  2776. ret = xmlUCSIsCatNo(codepoint);
  2777. break;
  2778. case XML_REGEXP_PUNCT:
  2779. ret = xmlUCSIsCatP(codepoint);
  2780. break;
  2781. case XML_REGEXP_PUNCT_CONNECTOR:
  2782. ret = xmlUCSIsCatPc(codepoint);
  2783. break;
  2784. case XML_REGEXP_PUNCT_DASH:
  2785. ret = xmlUCSIsCatPd(codepoint);
  2786. break;
  2787. case XML_REGEXP_PUNCT_OPEN:
  2788. ret = xmlUCSIsCatPs(codepoint);
  2789. break;
  2790. case XML_REGEXP_PUNCT_CLOSE:
  2791. ret = xmlUCSIsCatPe(codepoint);
  2792. break;
  2793. case XML_REGEXP_PUNCT_INITQUOTE:
  2794. ret = xmlUCSIsCatPi(codepoint);
  2795. break;
  2796. case XML_REGEXP_PUNCT_FINQUOTE:
  2797. ret = xmlUCSIsCatPf(codepoint);
  2798. break;
  2799. case XML_REGEXP_PUNCT_OTHERS:
  2800. ret = xmlUCSIsCatPo(codepoint);
  2801. break;
  2802. case XML_REGEXP_SEPAR:
  2803. ret = xmlUCSIsCatZ(codepoint);
  2804. break;
  2805. case XML_REGEXP_SEPAR_SPACE:
  2806. ret = xmlUCSIsCatZs(codepoint);
  2807. break;
  2808. case XML_REGEXP_SEPAR_LINE:
  2809. ret = xmlUCSIsCatZl(codepoint);
  2810. break;
  2811. case XML_REGEXP_SEPAR_PARA:
  2812. ret = xmlUCSIsCatZp(codepoint);
  2813. break;
  2814. case XML_REGEXP_SYMBOL:
  2815. ret = xmlUCSIsCatS(codepoint);
  2816. break;
  2817. case XML_REGEXP_SYMBOL_MATH:
  2818. ret = xmlUCSIsCatSm(codepoint);
  2819. break;
  2820. case XML_REGEXP_SYMBOL_CURRENCY:
  2821. ret = xmlUCSIsCatSc(codepoint);
  2822. break;
  2823. case XML_REGEXP_SYMBOL_MODIFIER:
  2824. ret = xmlUCSIsCatSk(codepoint);
  2825. break;
  2826. case XML_REGEXP_SYMBOL_OTHERS:
  2827. ret = xmlUCSIsCatSo(codepoint);
  2828. break;
  2829. case XML_REGEXP_OTHER:
  2830. ret = xmlUCSIsCatC(codepoint);
  2831. break;
  2832. case XML_REGEXP_OTHER_CONTROL:
  2833. ret = xmlUCSIsCatCc(codepoint);
  2834. break;
  2835. case XML_REGEXP_OTHER_FORMAT:
  2836. ret = xmlUCSIsCatCf(codepoint);
  2837. break;
  2838. case XML_REGEXP_OTHER_PRIVATE:
  2839. ret = xmlUCSIsCatCo(codepoint);
  2840. break;
  2841. case XML_REGEXP_OTHER_NA:
  2842. /* ret = xmlUCSIsCatCn(codepoint); */
  2843. /* Seems it doesn't exist anymore in recent Unicode releases */
  2844. ret = 0;
  2845. break;
  2846. case XML_REGEXP_BLOCK_NAME:
  2847. ret = xmlUCSIsBlock(codepoint, (const char *) blockName);
  2848. break;
  2849. }
  2850. if (neg)
  2851. return(!ret);
  2852. return(ret);
  2853. }
  2854. static int
  2855. xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint) {
  2856. int i, ret = 0;
  2857. xmlRegRangePtr range;
  2858. if ((atom == NULL) || (!IS_CHAR(codepoint)))
  2859. return(-1);
  2860. switch (atom->type) {
  2861. case XML_REGEXP_SUBREG:
  2862. case XML_REGEXP_EPSILON:
  2863. return(-1);
  2864. case XML_REGEXP_CHARVAL:
  2865. return(codepoint == atom->codepoint);
  2866. case XML_REGEXP_RANGES: {
  2867. int accept = 0;
  2868. for (i = 0;i < atom->nbRanges;i++) {
  2869. range = atom->ranges[i];
  2870. if (range->neg == 2) {
  2871. ret = xmlRegCheckCharacterRange(range->type, codepoint,
  2872. 0, range->start, range->end,
  2873. range->blockName);
  2874. if (ret != 0)
  2875. return(0); /* excluded char */
  2876. } else if (range->neg) {
  2877. ret = xmlRegCheckCharacterRange(range->type, codepoint,
  2878. 0, range->start, range->end,
  2879. range->blockName);
  2880. if (ret == 0)
  2881. accept = 1;
  2882. else
  2883. return(0);
  2884. } else {
  2885. ret = xmlRegCheckCharacterRange(range->type, codepoint,
  2886. 0, range->start, range->end,
  2887. range->blockName);
  2888. if (ret != 0)
  2889. accept = 1; /* might still be excluded */
  2890. }
  2891. }
  2892. return(accept);
  2893. }
  2894. case XML_REGEXP_STRING:
  2895. printf("TODO: XML_REGEXP_STRING\n");
  2896. return(-1);
  2897. case XML_REGEXP_ANYCHAR:
  2898. case XML_REGEXP_ANYSPACE:
  2899. case XML_REGEXP_NOTSPACE:
  2900. case XML_REGEXP_INITNAME:
  2901. case XML_REGEXP_NOTINITNAME:
  2902. case XML_REGEXP_NAMECHAR:
  2903. case XML_REGEXP_NOTNAMECHAR:
  2904. case XML_REGEXP_DECIMAL:
  2905. case XML_REGEXP_NOTDECIMAL:
  2906. case XML_REGEXP_REALCHAR:
  2907. case XML_REGEXP_NOTREALCHAR:
  2908. case XML_REGEXP_LETTER:
  2909. case XML_REGEXP_LETTER_UPPERCASE:
  2910. case XML_REGEXP_LETTER_LOWERCASE:
  2911. case XML_REGEXP_LETTER_TITLECASE:
  2912. case XML_REGEXP_LETTER_MODIFIER:
  2913. case XML_REGEXP_LETTER_OTHERS:
  2914. case XML_REGEXP_MARK:
  2915. case XML_REGEXP_MARK_NONSPACING:
  2916. case XML_REGEXP_MARK_SPACECOMBINING:
  2917. case XML_REGEXP_MARK_ENCLOSING:
  2918. case XML_REGEXP_NUMBER:
  2919. case XML_REGEXP_NUMBER_DECIMAL:
  2920. case XML_REGEXP_NUMBER_LETTER:
  2921. case XML_REGEXP_NUMBER_OTHERS:
  2922. case XML_REGEXP_PUNCT:
  2923. case XML_REGEXP_PUNCT_CONNECTOR:
  2924. case XML_REGEXP_PUNCT_DASH:
  2925. case XML_REGEXP_PUNCT_OPEN:
  2926. case XML_REGEXP_PUNCT_CLOSE:
  2927. case XML_REGEXP_PUNCT_INITQUOTE:
  2928. case XML_REGEXP_PUNCT_FINQUOTE:
  2929. case XML_REGEXP_PUNCT_OTHERS:
  2930. case XML_REGEXP_SEPAR:
  2931. case XML_REGEXP_SEPAR_SPACE:
  2932. case XML_REGEXP_SEPAR_LINE:
  2933. case XML_REGEXP_SEPAR_PARA:
  2934. case XML_REGEXP_SYMBOL:
  2935. case XML_REGEXP_SYMBOL_MATH:
  2936. case XML_REGEXP_SYMBOL_CURRENCY:
  2937. case XML_REGEXP_SYMBOL_MODIFIER:
  2938. case XML_REGEXP_SYMBOL_OTHERS:
  2939. case XML_REGEXP_OTHER:
  2940. case XML_REGEXP_OTHER_CONTROL:
  2941. case XML_REGEXP_OTHER_FORMAT:
  2942. case XML_REGEXP_OTHER_PRIVATE:
  2943. case XML_REGEXP_OTHER_NA:
  2944. case XML_REGEXP_BLOCK_NAME:
  2945. ret = xmlRegCheckCharacterRange(atom->type, codepoint, 0, 0, 0,
  2946. (const xmlChar *)atom->valuep);
  2947. if (atom->neg)
  2948. ret = !ret;
  2949. break;
  2950. }
  2951. return(ret);
  2952. }
  2953. /************************************************************************
  2954. * *
  2955. * Saving and restoring state of an execution context *
  2956. * *
  2957. ************************************************************************/
  2958. #ifdef DEBUG_REGEXP_EXEC
  2959. static void
  2960. xmlFARegDebugExec(xmlRegExecCtxtPtr exec) {
  2961. printf("state: %d:%d:idx %d", exec->state->no, exec->transno, exec->index);
  2962. if (exec->inputStack != NULL) {
  2963. int i;
  2964. printf(": ");
  2965. for (i = 0;(i < 3) && (i < exec->inputStackNr);i++)
  2966. printf("%s ", (const char *)
  2967. exec->inputStack[exec->inputStackNr - (i + 1)].value);
  2968. } else {
  2969. printf(": %s", &(exec->inputString[exec->index]));
  2970. }
  2971. printf("\n");
  2972. }
  2973. #endif
  2974. static void
  2975. xmlFARegExecSave(xmlRegExecCtxtPtr exec) {
  2976. #ifdef DEBUG_REGEXP_EXEC
  2977. printf("saving ");
  2978. exec->transno++;
  2979. xmlFARegDebugExec(exec);
  2980. exec->transno--;
  2981. #endif
  2982. #ifdef MAX_PUSH
  2983. if (exec->nbPush > MAX_PUSH) {
  2984. return;
  2985. }
  2986. exec->nbPush++;
  2987. #endif
  2988. if (exec->maxRollbacks == 0) {
  2989. exec->maxRollbacks = 4;
  2990. exec->rollbacks = (xmlRegExecRollback *) xmlMalloc(exec->maxRollbacks *
  2991. sizeof(xmlRegExecRollback));
  2992. if (exec->rollbacks == NULL) {
  2993. xmlRegexpErrMemory(NULL, "saving regexp");
  2994. exec->maxRollbacks = 0;
  2995. return;
  2996. }
  2997. memset(exec->rollbacks, 0,
  2998. exec->maxRollbacks * sizeof(xmlRegExecRollback));
  2999. } else if (exec->nbRollbacks >= exec->maxRollbacks) {
  3000. xmlRegExecRollback *tmp;
  3001. int len = exec->maxRollbacks;
  3002. exec->maxRollbacks *= 2;
  3003. tmp = (xmlRegExecRollback *) xmlRealloc(exec->rollbacks,
  3004. exec->maxRollbacks * sizeof(xmlRegExecRollback));
  3005. if (tmp == NULL) {
  3006. xmlRegexpErrMemory(NULL, "saving regexp");
  3007. exec->maxRollbacks /= 2;
  3008. return;
  3009. }
  3010. exec->rollbacks = tmp;
  3011. tmp = &exec->rollbacks[len];
  3012. memset(tmp, 0, (exec->maxRollbacks - len) * sizeof(xmlRegExecRollback));
  3013. }
  3014. exec->rollbacks[exec->nbRollbacks].state = exec->state;
  3015. exec->rollbacks[exec->nbRollbacks].index = exec->index;
  3016. exec->rollbacks[exec->nbRollbacks].nextbranch = exec->transno + 1;
  3017. if (exec->comp->nbCounters > 0) {
  3018. if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
  3019. exec->rollbacks[exec->nbRollbacks].counts = (int *)
  3020. xmlMalloc(exec->comp->nbCounters * sizeof(int));
  3021. if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
  3022. xmlRegexpErrMemory(NULL, "saving regexp");
  3023. exec->status = -5;
  3024. return;
  3025. }
  3026. }
  3027. memcpy(exec->rollbacks[exec->nbRollbacks].counts, exec->counts,
  3028. exec->comp->nbCounters * sizeof(int));
  3029. }
  3030. exec->nbRollbacks++;
  3031. }
  3032. static void
  3033. xmlFARegExecRollBack(xmlRegExecCtxtPtr exec) {
  3034. if (exec->nbRollbacks <= 0) {
  3035. exec->status = -1;
  3036. #ifdef DEBUG_REGEXP_EXEC
  3037. printf("rollback failed on empty stack\n");
  3038. #endif
  3039. return;
  3040. }
  3041. exec->nbRollbacks--;
  3042. exec->state = exec->rollbacks[exec->nbRollbacks].state;
  3043. exec->index = exec->rollbacks[exec->nbRollbacks].index;
  3044. exec->transno = exec->rollbacks[exec->nbRollbacks].nextbranch;
  3045. if (exec->comp->nbCounters > 0) {
  3046. if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
  3047. fprintf(stderr, "exec save: allocation failed");
  3048. exec->status = -6;
  3049. return;
  3050. }
  3051. if (exec->counts) {
  3052. memcpy(exec->counts, exec->rollbacks[exec->nbRollbacks].counts,
  3053. exec->comp->nbCounters * sizeof(int));
  3054. }
  3055. }
  3056. #ifdef DEBUG_REGEXP_EXEC
  3057. printf("restored ");
  3058. xmlFARegDebugExec(exec);
  3059. #endif
  3060. }
  3061. /************************************************************************
  3062. * *
  3063. * Verifier, running an input against a compiled regexp *
  3064. * *
  3065. ************************************************************************/
  3066. static int
  3067. xmlFARegExec(xmlRegexpPtr comp, const xmlChar *content) {
  3068. xmlRegExecCtxt execval;
  3069. xmlRegExecCtxtPtr exec = &execval;
  3070. int ret, codepoint = 0, len, deter;
  3071. exec->inputString = content;
  3072. exec->index = 0;
  3073. exec->nbPush = 0;
  3074. exec->determinist = 1;
  3075. exec->maxRollbacks = 0;
  3076. exec->nbRollbacks = 0;
  3077. exec->rollbacks = NULL;
  3078. exec->status = 0;
  3079. exec->comp = comp;
  3080. exec->state = comp->states[0];
  3081. exec->transno = 0;
  3082. exec->transcount = 0;
  3083. exec->inputStack = NULL;
  3084. exec->inputStackMax = 0;
  3085. if (comp->nbCounters > 0) {
  3086. exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int));
  3087. if (exec->counts == NULL) {
  3088. xmlRegexpErrMemory(NULL, "running regexp");
  3089. return(-1);
  3090. }
  3091. memset(exec->counts, 0, comp->nbCounters * sizeof(int));
  3092. } else
  3093. exec->counts = NULL;
  3094. while ((exec->status == 0) && (exec->state != NULL) &&
  3095. ((exec->inputString[exec->index] != 0) ||
  3096. ((exec->state != NULL) &&
  3097. (exec->state->type != XML_REGEXP_FINAL_STATE)))) {
  3098. xmlRegTransPtr trans;
  3099. xmlRegAtomPtr atom;
  3100. /*
  3101. * If end of input on non-terminal state, rollback, however we may
  3102. * still have epsilon like transition for counted transitions
  3103. * on counters, in that case don't break too early. Additionally,
  3104. * if we are working on a range like "AB{0,2}", where B is not present,
  3105. * we don't want to break.
  3106. */
  3107. len = 1;
  3108. if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL)) {
  3109. /*
  3110. * if there is a transition, we must check if
  3111. * atom allows minOccurs of 0
  3112. */
  3113. if (exec->transno < exec->state->nbTrans) {
  3114. trans = &exec->state->trans[exec->transno];
  3115. if (trans->to >=0) {
  3116. atom = trans->atom;
  3117. if (!((atom->min == 0) && (atom->max > 0)))
  3118. goto rollback;
  3119. }
  3120. } else
  3121. goto rollback;
  3122. }
  3123. exec->transcount = 0;
  3124. for (;exec->transno < exec->state->nbTrans;exec->transno++) {
  3125. trans = &exec->state->trans[exec->transno];
  3126. if (trans->to < 0)
  3127. continue;
  3128. atom = trans->atom;
  3129. ret = 0;
  3130. deter = 1;
  3131. if (trans->count >= 0) {
  3132. int count;
  3133. xmlRegCounterPtr counter;
  3134. if (exec->counts == NULL) {
  3135. exec->status = -1;
  3136. goto error;
  3137. }
  3138. /*
  3139. * A counted transition.
  3140. */
  3141. count = exec->counts[trans->count];
  3142. counter = &exec->comp->counters[trans->count];
  3143. #ifdef DEBUG_REGEXP_EXEC
  3144. printf("testing count %d: val %d, min %d, max %d\n",
  3145. trans->count, count, counter->min, counter->max);
  3146. #endif
  3147. ret = ((count >= counter->min) && (count <= counter->max));
  3148. if ((ret) && (counter->min != counter->max))
  3149. deter = 0;
  3150. } else if (atom == NULL) {
  3151. fprintf(stderr, "epsilon transition left at runtime\n");
  3152. exec->status = -2;
  3153. break;
  3154. } else if (exec->inputString[exec->index] != 0) {
  3155. codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
  3156. ret = xmlRegCheckCharacter(atom, codepoint);
  3157. if ((ret == 1) && (atom->min >= 0) && (atom->max > 0)) {
  3158. xmlRegStatePtr to = comp->states[trans->to];
  3159. /*
  3160. * this is a multiple input sequence
  3161. * If there is a counter associated increment it now.
  3162. * before potentially saving and rollback
  3163. * do not increment if the counter is already over the
  3164. * maximum limit in which case get to next transition
  3165. */
  3166. if (trans->counter >= 0) {
  3167. xmlRegCounterPtr counter;
  3168. if ((exec->counts == NULL) ||
  3169. (exec->comp == NULL) ||
  3170. (exec->comp->counters == NULL)) {
  3171. exec->status = -1;
  3172. goto error;
  3173. }
  3174. counter = &exec->comp->counters[trans->counter];
  3175. if (exec->counts[trans->counter] >= counter->max)
  3176. continue; /* for loop on transitions */
  3177. #ifdef DEBUG_REGEXP_EXEC
  3178. printf("Increasing count %d\n", trans->counter);
  3179. #endif
  3180. exec->counts[trans->counter]++;
  3181. }
  3182. if (exec->state->nbTrans > exec->transno + 1) {
  3183. xmlFARegExecSave(exec);
  3184. }
  3185. exec->transcount = 1;
  3186. do {
  3187. /*
  3188. * Try to progress as much as possible on the input
  3189. */
  3190. if (exec->transcount == atom->max) {
  3191. break;
  3192. }
  3193. exec->index += len;
  3194. /*
  3195. * End of input: stop here
  3196. */
  3197. if (exec->inputString[exec->index] == 0) {
  3198. exec->index -= len;
  3199. break;
  3200. }
  3201. if (exec->transcount >= atom->min) {
  3202. int transno = exec->transno;
  3203. xmlRegStatePtr state = exec->state;
  3204. /*
  3205. * The transition is acceptable save it
  3206. */
  3207. exec->transno = -1; /* trick */
  3208. exec->state = to;
  3209. xmlFARegExecSave(exec);
  3210. exec->transno = transno;
  3211. exec->state = state;
  3212. }
  3213. codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
  3214. len);
  3215. ret = xmlRegCheckCharacter(atom, codepoint);
  3216. exec->transcount++;
  3217. } while (ret == 1);
  3218. if (exec->transcount < atom->min)
  3219. ret = 0;
  3220. /*
  3221. * If the last check failed but one transition was found
  3222. * possible, rollback
  3223. */
  3224. if (ret < 0)
  3225. ret = 0;
  3226. if (ret == 0) {
  3227. goto rollback;
  3228. }
  3229. if (trans->counter >= 0) {
  3230. if (exec->counts == NULL) {
  3231. exec->status = -1;
  3232. goto error;
  3233. }
  3234. #ifdef DEBUG_REGEXP_EXEC
  3235. printf("Decreasing count %d\n", trans->counter);
  3236. #endif
  3237. exec->counts[trans->counter]--;
  3238. }
  3239. } else if ((ret == 0) && (atom->min == 0) && (atom->max > 0)) {
  3240. /*
  3241. * we don't match on the codepoint, but minOccurs of 0
  3242. * says that's ok. Setting len to 0 inhibits stepping
  3243. * over the codepoint.
  3244. */
  3245. exec->transcount = 1;
  3246. len = 0;
  3247. ret = 1;
  3248. }
  3249. } else if ((atom->min == 0) && (atom->max > 0)) {
  3250. /* another spot to match when minOccurs is 0 */
  3251. exec->transcount = 1;
  3252. len = 0;
  3253. ret = 1;
  3254. }
  3255. if (ret == 1) {
  3256. if ((trans->nd == 1) ||
  3257. ((trans->count >= 0) && (deter == 0) &&
  3258. (exec->state->nbTrans > exec->transno + 1))) {
  3259. #ifdef DEBUG_REGEXP_EXEC
  3260. if (trans->nd == 1)
  3261. printf("Saving on nd transition atom %d for %c at %d\n",
  3262. trans->atom->no, codepoint, exec->index);
  3263. else
  3264. printf("Saving on counted transition count %d for %c at %d\n",
  3265. trans->count, codepoint, exec->index);
  3266. #endif
  3267. xmlFARegExecSave(exec);
  3268. }
  3269. if (trans->counter >= 0) {
  3270. xmlRegCounterPtr counter;
  3271. /* make sure we don't go over the counter maximum value */
  3272. if ((exec->counts == NULL) ||
  3273. (exec->comp == NULL) ||
  3274. (exec->comp->counters == NULL)) {
  3275. exec->status = -1;
  3276. goto error;
  3277. }
  3278. counter = &exec->comp->counters[trans->counter];
  3279. if (exec->counts[trans->counter] >= counter->max)
  3280. continue; /* for loop on transitions */
  3281. #ifdef DEBUG_REGEXP_EXEC
  3282. printf("Increasing count %d\n", trans->counter);
  3283. #endif
  3284. exec->counts[trans->counter]++;
  3285. }
  3286. if ((trans->count >= 0) &&
  3287. (trans->count < REGEXP_ALL_COUNTER)) {
  3288. if (exec->counts == NULL) {
  3289. exec->status = -1;
  3290. goto error;
  3291. }
  3292. #ifdef DEBUG_REGEXP_EXEC
  3293. printf("resetting count %d on transition\n",
  3294. trans->count);
  3295. #endif
  3296. exec->counts[trans->count] = 0;
  3297. }
  3298. #ifdef DEBUG_REGEXP_EXEC
  3299. printf("entering state %d\n", trans->to);
  3300. #endif
  3301. exec->state = comp->states[trans->to];
  3302. exec->transno = 0;
  3303. if (trans->atom != NULL) {
  3304. exec->index += len;
  3305. }
  3306. goto progress;
  3307. } else if (ret < 0) {
  3308. exec->status = -4;
  3309. break;
  3310. }
  3311. }
  3312. if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
  3313. rollback:
  3314. /*
  3315. * Failed to find a way out
  3316. */
  3317. exec->determinist = 0;
  3318. #ifdef DEBUG_REGEXP_EXEC
  3319. printf("rollback from state %d on %d:%c\n", exec->state->no,
  3320. codepoint,codepoint);
  3321. #endif
  3322. xmlFARegExecRollBack(exec);
  3323. }
  3324. progress:
  3325. continue;
  3326. }
  3327. error:
  3328. if (exec->rollbacks != NULL) {
  3329. if (exec->counts != NULL) {
  3330. int i;
  3331. for (i = 0;i < exec->maxRollbacks;i++)
  3332. if (exec->rollbacks[i].counts != NULL)
  3333. xmlFree(exec->rollbacks[i].counts);
  3334. }
  3335. xmlFree(exec->rollbacks);
  3336. }
  3337. if (exec->state == NULL)
  3338. return(-1);
  3339. if (exec->counts != NULL)
  3340. xmlFree(exec->counts);
  3341. if (exec->status == 0)
  3342. return(1);
  3343. if (exec->status == -1) {
  3344. if (exec->nbPush > MAX_PUSH)
  3345. return(-1);
  3346. return(0);
  3347. }
  3348. return(exec->status);
  3349. }
  3350. /************************************************************************
  3351. * *
  3352. * Progressive interface to the verifier one atom at a time *
  3353. * *
  3354. ************************************************************************/
  3355. #ifdef DEBUG_ERR
  3356. static void testerr(xmlRegExecCtxtPtr exec);
  3357. #endif
  3358. /**
  3359. * xmlRegNewExecCtxt:
  3360. * @comp: a precompiled regular expression
  3361. * @callback: a callback function used for handling progresses in the
  3362. * automata matching phase
  3363. * @data: the context data associated to the callback in this context
  3364. *
  3365. * Build a context used for progressive evaluation of a regexp.
  3366. *
  3367. * Returns the new context
  3368. */
  3369. xmlRegExecCtxtPtr
  3370. xmlRegNewExecCtxt(xmlRegexpPtr comp, xmlRegExecCallbacks callback, void *data) {
  3371. xmlRegExecCtxtPtr exec;
  3372. if (comp == NULL)
  3373. return(NULL);
  3374. if ((comp->compact == NULL) && (comp->states == NULL))
  3375. return(NULL);
  3376. exec = (xmlRegExecCtxtPtr) xmlMalloc(sizeof(xmlRegExecCtxt));
  3377. if (exec == NULL) {
  3378. xmlRegexpErrMemory(NULL, "creating execution context");
  3379. return(NULL);
  3380. }
  3381. memset(exec, 0, sizeof(xmlRegExecCtxt));
  3382. exec->inputString = NULL;
  3383. exec->index = 0;
  3384. exec->determinist = 1;
  3385. exec->maxRollbacks = 0;
  3386. exec->nbRollbacks = 0;
  3387. exec->rollbacks = NULL;
  3388. exec->status = 0;
  3389. exec->comp = comp;
  3390. if (comp->compact == NULL)
  3391. exec->state = comp->states[0];
  3392. exec->transno = 0;
  3393. exec->transcount = 0;
  3394. exec->callback = callback;
  3395. exec->data = data;
  3396. if (comp->nbCounters > 0) {
  3397. /*
  3398. * For error handling, exec->counts is allocated twice the size
  3399. * the second half is used to store the data in case of rollback
  3400. */
  3401. exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int)
  3402. * 2);
  3403. if (exec->counts == NULL) {
  3404. xmlRegexpErrMemory(NULL, "creating execution context");
  3405. xmlFree(exec);
  3406. return(NULL);
  3407. }
  3408. memset(exec->counts, 0, comp->nbCounters * sizeof(int) * 2);
  3409. exec->errCounts = &exec->counts[comp->nbCounters];
  3410. } else {
  3411. exec->counts = NULL;
  3412. exec->errCounts = NULL;
  3413. }
  3414. exec->inputStackMax = 0;
  3415. exec->inputStackNr = 0;
  3416. exec->inputStack = NULL;
  3417. exec->errStateNo = -1;
  3418. exec->errString = NULL;
  3419. exec->nbPush = 0;
  3420. return(exec);
  3421. }
  3422. /**
  3423. * xmlRegFreeExecCtxt:
  3424. * @exec: a regular expression evaluation context
  3425. *
  3426. * Free the structures associated to a regular expression evaluation context.
  3427. */
  3428. void
  3429. xmlRegFreeExecCtxt(xmlRegExecCtxtPtr exec) {
  3430. if (exec == NULL)
  3431. return;
  3432. if (exec->rollbacks != NULL) {
  3433. if (exec->counts != NULL) {
  3434. int i;
  3435. for (i = 0;i < exec->maxRollbacks;i++)
  3436. if (exec->rollbacks[i].counts != NULL)
  3437. xmlFree(exec->rollbacks[i].counts);
  3438. }
  3439. xmlFree(exec->rollbacks);
  3440. }
  3441. if (exec->counts != NULL)
  3442. xmlFree(exec->counts);
  3443. if (exec->inputStack != NULL) {
  3444. int i;
  3445. for (i = 0;i < exec->inputStackNr;i++) {
  3446. if (exec->inputStack[i].value != NULL)
  3447. xmlFree(exec->inputStack[i].value);
  3448. }
  3449. xmlFree(exec->inputStack);
  3450. }
  3451. if (exec->errString != NULL)
  3452. xmlFree(exec->errString);
  3453. xmlFree(exec);
  3454. }
  3455. static void
  3456. xmlFARegExecSaveInputString(xmlRegExecCtxtPtr exec, const xmlChar *value,
  3457. void *data) {
  3458. #ifdef DEBUG_PUSH
  3459. printf("saving value: %d:%s\n", exec->inputStackNr, value);
  3460. #endif
  3461. if (exec->inputStackMax == 0) {
  3462. exec->inputStackMax = 4;
  3463. exec->inputStack = (xmlRegInputTokenPtr)
  3464. xmlMalloc(exec->inputStackMax * sizeof(xmlRegInputToken));
  3465. if (exec->inputStack == NULL) {
  3466. xmlRegexpErrMemory(NULL, "pushing input string");
  3467. exec->inputStackMax = 0;
  3468. return;
  3469. }
  3470. } else if (exec->inputStackNr + 1 >= exec->inputStackMax) {
  3471. xmlRegInputTokenPtr tmp;
  3472. exec->inputStackMax *= 2;
  3473. tmp = (xmlRegInputTokenPtr) xmlRealloc(exec->inputStack,
  3474. exec->inputStackMax * sizeof(xmlRegInputToken));
  3475. if (tmp == NULL) {
  3476. xmlRegexpErrMemory(NULL, "pushing input string");
  3477. exec->inputStackMax /= 2;
  3478. return;
  3479. }
  3480. exec->inputStack = tmp;
  3481. }
  3482. exec->inputStack[exec->inputStackNr].value = xmlStrdup(value);
  3483. exec->inputStack[exec->inputStackNr].data = data;
  3484. exec->inputStackNr++;
  3485. exec->inputStack[exec->inputStackNr].value = NULL;
  3486. exec->inputStack[exec->inputStackNr].data = NULL;
  3487. }
  3488. /**
  3489. * xmlRegStrEqualWildcard:
  3490. * @expStr: the string to be evaluated
  3491. * @valStr: the validation string
  3492. *
  3493. * Checks if both strings are equal or have the same content. "*"
  3494. * can be used as a wildcard in @valStr; "|" is used as a separator of
  3495. * substrings in both @expStr and @valStr.
  3496. *
  3497. * Returns 1 if the comparison is satisfied and the number of substrings
  3498. * is equal, 0 otherwise.
  3499. */
  3500. static int
  3501. xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr) {
  3502. if (expStr == valStr) return(1);
  3503. if (expStr == NULL) return(0);
  3504. if (valStr == NULL) return(0);
  3505. do {
  3506. /*
  3507. * Eval if we have a wildcard for the current item.
  3508. */
  3509. if (*expStr != *valStr) {
  3510. /* if one of them starts with a wildcard make valStr be it */
  3511. if (*valStr == '*') {
  3512. const xmlChar *tmp;
  3513. tmp = valStr;
  3514. valStr = expStr;
  3515. expStr = tmp;
  3516. }
  3517. if ((*valStr != 0) && (*expStr != 0) && (*expStr++ == '*')) {
  3518. do {
  3519. if (*valStr == XML_REG_STRING_SEPARATOR)
  3520. break;
  3521. valStr++;
  3522. } while (*valStr != 0);
  3523. continue;
  3524. } else
  3525. return(0);
  3526. }
  3527. expStr++;
  3528. valStr++;
  3529. } while (*valStr != 0);
  3530. if (*expStr != 0)
  3531. return (0);
  3532. else
  3533. return (1);
  3534. }
  3535. /**
  3536. * xmlRegCompactPushString:
  3537. * @exec: a regexp execution context
  3538. * @comp: the precompiled exec with a compact table
  3539. * @value: a string token input
  3540. * @data: data associated to the token to reuse in callbacks
  3541. *
  3542. * Push one input token in the execution context
  3543. *
  3544. * Returns: 1 if the regexp reached a final state, 0 if non-final, and
  3545. * a negative value in case of error.
  3546. */
  3547. static int
  3548. xmlRegCompactPushString(xmlRegExecCtxtPtr exec,
  3549. xmlRegexpPtr comp,
  3550. const xmlChar *value,
  3551. void *data) {
  3552. int state = exec->index;
  3553. int i, target;
  3554. if ((comp == NULL) || (comp->compact == NULL) || (comp->stringMap == NULL))
  3555. return(-1);
  3556. if (value == NULL) {
  3557. /*
  3558. * are we at a final state ?
  3559. */
  3560. if (comp->compact[state * (comp->nbstrings + 1)] ==
  3561. XML_REGEXP_FINAL_STATE)
  3562. return(1);
  3563. return(0);
  3564. }
  3565. #ifdef DEBUG_PUSH
  3566. printf("value pushed: %s\n", value);
  3567. #endif
  3568. /*
  3569. * Examine all outside transitions from current state
  3570. */
  3571. for (i = 0;i < comp->nbstrings;i++) {
  3572. target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
  3573. if ((target > 0) && (target <= comp->nbstates)) {
  3574. target--; /* to avoid 0 */
  3575. if (xmlRegStrEqualWildcard(comp->stringMap[i], value)) {
  3576. exec->index = target;
  3577. if ((exec->callback != NULL) && (comp->transdata != NULL)) {
  3578. exec->callback(exec->data, value,
  3579. comp->transdata[state * comp->nbstrings + i], data);
  3580. }
  3581. #ifdef DEBUG_PUSH
  3582. printf("entering state %d\n", target);
  3583. #endif
  3584. if (comp->compact[target * (comp->nbstrings + 1)] ==
  3585. XML_REGEXP_SINK_STATE)
  3586. goto error;
  3587. if (comp->compact[target * (comp->nbstrings + 1)] ==
  3588. XML_REGEXP_FINAL_STATE)
  3589. return(1);
  3590. return(0);
  3591. }
  3592. }
  3593. }
  3594. /*
  3595. * Failed to find an exit transition out from current state for the
  3596. * current token
  3597. */
  3598. #ifdef DEBUG_PUSH
  3599. printf("failed to find a transition for %s on state %d\n", value, state);
  3600. #endif
  3601. error:
  3602. if (exec->errString != NULL)
  3603. xmlFree(exec->errString);
  3604. exec->errString = xmlStrdup(value);
  3605. exec->errStateNo = state;
  3606. exec->status = -1;
  3607. #ifdef DEBUG_ERR
  3608. testerr(exec);
  3609. #endif
  3610. return(-1);
  3611. }
  3612. /**
  3613. * xmlRegExecPushStringInternal:
  3614. * @exec: a regexp execution context or NULL to indicate the end
  3615. * @value: a string token input
  3616. * @data: data associated to the token to reuse in callbacks
  3617. * @compound: value was assembled from 2 strings
  3618. *
  3619. * Push one input token in the execution context
  3620. *
  3621. * Returns: 1 if the regexp reached a final state, 0 if non-final, and
  3622. * a negative value in case of error.
  3623. */
  3624. static int
  3625. xmlRegExecPushStringInternal(xmlRegExecCtxtPtr exec, const xmlChar *value,
  3626. void *data, int compound) {
  3627. xmlRegTransPtr trans;
  3628. xmlRegAtomPtr atom;
  3629. int ret;
  3630. int final = 0;
  3631. int progress = 1;
  3632. if (exec == NULL)
  3633. return(-1);
  3634. if (exec->comp == NULL)
  3635. return(-1);
  3636. if (exec->status != 0)
  3637. return(exec->status);
  3638. if (exec->comp->compact != NULL)
  3639. return(xmlRegCompactPushString(exec, exec->comp, value, data));
  3640. if (value == NULL) {
  3641. if (exec->state->type == XML_REGEXP_FINAL_STATE)
  3642. return(1);
  3643. final = 1;
  3644. }
  3645. #ifdef DEBUG_PUSH
  3646. printf("value pushed: %s\n", value);
  3647. #endif
  3648. /*
  3649. * If we have an active rollback stack push the new value there
  3650. * and get back to where we were left
  3651. */
  3652. if ((value != NULL) && (exec->inputStackNr > 0)) {
  3653. xmlFARegExecSaveInputString(exec, value, data);
  3654. value = exec->inputStack[exec->index].value;
  3655. data = exec->inputStack[exec->index].data;
  3656. #ifdef DEBUG_PUSH
  3657. printf("value loaded: %s\n", value);
  3658. #endif
  3659. }
  3660. while ((exec->status == 0) &&
  3661. ((value != NULL) ||
  3662. ((final == 1) &&
  3663. (exec->state->type != XML_REGEXP_FINAL_STATE)))) {
  3664. /*
  3665. * End of input on non-terminal state, rollback, however we may
  3666. * still have epsilon like transition for counted transitions
  3667. * on counters, in that case don't break too early.
  3668. */
  3669. if ((value == NULL) && (exec->counts == NULL))
  3670. goto rollback;
  3671. exec->transcount = 0;
  3672. for (;exec->transno < exec->state->nbTrans;exec->transno++) {
  3673. trans = &exec->state->trans[exec->transno];
  3674. if (trans->to < 0)
  3675. continue;
  3676. atom = trans->atom;
  3677. ret = 0;
  3678. if (trans->count == REGEXP_ALL_LAX_COUNTER) {
  3679. int i;
  3680. int count;
  3681. xmlRegTransPtr t;
  3682. xmlRegCounterPtr counter;
  3683. ret = 0;
  3684. #ifdef DEBUG_PUSH
  3685. printf("testing all lax %d\n", trans->count);
  3686. #endif
  3687. /*
  3688. * Check all counted transitions from the current state
  3689. */
  3690. if ((value == NULL) && (final)) {
  3691. ret = 1;
  3692. } else if (value != NULL) {
  3693. for (i = 0;i < exec->state->nbTrans;i++) {
  3694. t = &exec->state->trans[i];
  3695. if ((t->counter < 0) || (t == trans))
  3696. continue;
  3697. counter = &exec->comp->counters[t->counter];
  3698. count = exec->counts[t->counter];
  3699. if ((count < counter->max) &&
  3700. (t->atom != NULL) &&
  3701. (xmlStrEqual(value, t->atom->valuep))) {
  3702. ret = 0;
  3703. break;
  3704. }
  3705. if ((count >= counter->min) &&
  3706. (count < counter->max) &&
  3707. (t->atom != NULL) &&
  3708. (xmlStrEqual(value, t->atom->valuep))) {
  3709. ret = 1;
  3710. break;
  3711. }
  3712. }
  3713. }
  3714. } else if (trans->count == REGEXP_ALL_COUNTER) {
  3715. int i;
  3716. int count;
  3717. xmlRegTransPtr t;
  3718. xmlRegCounterPtr counter;
  3719. ret = 1;
  3720. #ifdef DEBUG_PUSH
  3721. printf("testing all %d\n", trans->count);
  3722. #endif
  3723. /*
  3724. * Check all counted transitions from the current state
  3725. */
  3726. for (i = 0;i < exec->state->nbTrans;i++) {
  3727. t = &exec->state->trans[i];
  3728. if ((t->counter < 0) || (t == trans))
  3729. continue;
  3730. counter = &exec->comp->counters[t->counter];
  3731. count = exec->counts[t->counter];
  3732. if ((count < counter->min) || (count > counter->max)) {
  3733. ret = 0;
  3734. break;
  3735. }
  3736. }
  3737. } else if (trans->count >= 0) {
  3738. int count;
  3739. xmlRegCounterPtr counter;
  3740. /*
  3741. * A counted transition.
  3742. */
  3743. count = exec->counts[trans->count];
  3744. counter = &exec->comp->counters[trans->count];
  3745. #ifdef DEBUG_PUSH
  3746. printf("testing count %d: val %d, min %d, max %d\n",
  3747. trans->count, count, counter->min, counter->max);
  3748. #endif
  3749. ret = ((count >= counter->min) && (count <= counter->max));
  3750. } else if (atom == NULL) {
  3751. fprintf(stderr, "epsilon transition left at runtime\n");
  3752. exec->status = -2;
  3753. break;
  3754. } else if (value != NULL) {
  3755. ret = xmlRegStrEqualWildcard(atom->valuep, value);
  3756. if (atom->neg) {
  3757. ret = !ret;
  3758. if (!compound)
  3759. ret = 0;
  3760. }
  3761. if ((ret == 1) && (trans->counter >= 0)) {
  3762. xmlRegCounterPtr counter;
  3763. int count;
  3764. count = exec->counts[trans->counter];
  3765. counter = &exec->comp->counters[trans->counter];
  3766. if (count >= counter->max)
  3767. ret = 0;
  3768. }
  3769. if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
  3770. xmlRegStatePtr to = exec->comp->states[trans->to];
  3771. /*
  3772. * this is a multiple input sequence
  3773. */
  3774. if (exec->state->nbTrans > exec->transno + 1) {
  3775. if (exec->inputStackNr <= 0) {
  3776. xmlFARegExecSaveInputString(exec, value, data);
  3777. }
  3778. xmlFARegExecSave(exec);
  3779. }
  3780. exec->transcount = 1;
  3781. do {
  3782. /*
  3783. * Try to progress as much as possible on the input
  3784. */
  3785. if (exec->transcount == atom->max) {
  3786. break;
  3787. }
  3788. exec->index++;
  3789. value = exec->inputStack[exec->index].value;
  3790. data = exec->inputStack[exec->index].data;
  3791. #ifdef DEBUG_PUSH
  3792. printf("value loaded: %s\n", value);
  3793. #endif
  3794. /*
  3795. * End of input: stop here
  3796. */
  3797. if (value == NULL) {
  3798. exec->index --;
  3799. break;
  3800. }
  3801. if (exec->transcount >= atom->min) {
  3802. int transno = exec->transno;
  3803. xmlRegStatePtr state = exec->state;
  3804. /*
  3805. * The transition is acceptable save it
  3806. */
  3807. exec->transno = -1; /* trick */
  3808. exec->state = to;
  3809. if (exec->inputStackNr <= 0) {
  3810. xmlFARegExecSaveInputString(exec, value, data);
  3811. }
  3812. xmlFARegExecSave(exec);
  3813. exec->transno = transno;
  3814. exec->state = state;
  3815. }
  3816. ret = xmlStrEqual(value, atom->valuep);
  3817. exec->transcount++;
  3818. } while (ret == 1);
  3819. if (exec->transcount < atom->min)
  3820. ret = 0;
  3821. /*
  3822. * If the last check failed but one transition was found
  3823. * possible, rollback
  3824. */
  3825. if (ret < 0)
  3826. ret = 0;
  3827. if (ret == 0) {
  3828. goto rollback;
  3829. }
  3830. }
  3831. }
  3832. if (ret == 1) {
  3833. if ((exec->callback != NULL) && (atom != NULL) &&
  3834. (data != NULL)) {
  3835. exec->callback(exec->data, atom->valuep,
  3836. atom->data, data);
  3837. }
  3838. if (exec->state->nbTrans > exec->transno + 1) {
  3839. if (exec->inputStackNr <= 0) {
  3840. xmlFARegExecSaveInputString(exec, value, data);
  3841. }
  3842. xmlFARegExecSave(exec);
  3843. }
  3844. if (trans->counter >= 0) {
  3845. #ifdef DEBUG_PUSH
  3846. printf("Increasing count %d\n", trans->counter);
  3847. #endif
  3848. exec->counts[trans->counter]++;
  3849. }
  3850. if ((trans->count >= 0) &&
  3851. (trans->count < REGEXP_ALL_COUNTER)) {
  3852. #ifdef DEBUG_REGEXP_EXEC
  3853. printf("resetting count %d on transition\n",
  3854. trans->count);
  3855. #endif
  3856. exec->counts[trans->count] = 0;
  3857. }
  3858. #ifdef DEBUG_PUSH
  3859. printf("entering state %d\n", trans->to);
  3860. #endif
  3861. if ((exec->comp->states[trans->to] != NULL) &&
  3862. (exec->comp->states[trans->to]->type ==
  3863. XML_REGEXP_SINK_STATE)) {
  3864. /*
  3865. * entering a sink state, save the current state as error
  3866. * state.
  3867. */
  3868. if (exec->errString != NULL)
  3869. xmlFree(exec->errString);
  3870. exec->errString = xmlStrdup(value);
  3871. exec->errState = exec->state;
  3872. memcpy(exec->errCounts, exec->counts,
  3873. exec->comp->nbCounters * sizeof(int));
  3874. }
  3875. exec->state = exec->comp->states[trans->to];
  3876. exec->transno = 0;
  3877. if (trans->atom != NULL) {
  3878. if (exec->inputStack != NULL) {
  3879. exec->index++;
  3880. if (exec->index < exec->inputStackNr) {
  3881. value = exec->inputStack[exec->index].value;
  3882. data = exec->inputStack[exec->index].data;
  3883. #ifdef DEBUG_PUSH
  3884. printf("value loaded: %s\n", value);
  3885. #endif
  3886. } else {
  3887. value = NULL;
  3888. data = NULL;
  3889. #ifdef DEBUG_PUSH
  3890. printf("end of input\n");
  3891. #endif
  3892. }
  3893. } else {
  3894. value = NULL;
  3895. data = NULL;
  3896. #ifdef DEBUG_PUSH
  3897. printf("end of input\n");
  3898. #endif
  3899. }
  3900. }
  3901. goto progress;
  3902. } else if (ret < 0) {
  3903. exec->status = -4;
  3904. break;
  3905. }
  3906. }
  3907. if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
  3908. rollback:
  3909. /*
  3910. * if we didn't yet rollback on the current input
  3911. * store the current state as the error state.
  3912. */
  3913. if ((progress) && (exec->state != NULL) &&
  3914. (exec->state->type != XML_REGEXP_SINK_STATE)) {
  3915. progress = 0;
  3916. if (exec->errString != NULL)
  3917. xmlFree(exec->errString);
  3918. exec->errString = xmlStrdup(value);
  3919. exec->errState = exec->state;
  3920. if (exec->comp->nbCounters)
  3921. memcpy(exec->errCounts, exec->counts,
  3922. exec->comp->nbCounters * sizeof(int));
  3923. }
  3924. /*
  3925. * Failed to find a way out
  3926. */
  3927. exec->determinist = 0;
  3928. xmlFARegExecRollBack(exec);
  3929. if ((exec->inputStack != NULL ) && (exec->status == 0)) {
  3930. value = exec->inputStack[exec->index].value;
  3931. data = exec->inputStack[exec->index].data;
  3932. #ifdef DEBUG_PUSH
  3933. printf("value loaded: %s\n", value);
  3934. #endif
  3935. }
  3936. }
  3937. continue;
  3938. progress:
  3939. progress = 1;
  3940. continue;
  3941. }
  3942. if (exec->status == 0) {
  3943. return(exec->state->type == XML_REGEXP_FINAL_STATE);
  3944. }
  3945. #ifdef DEBUG_ERR
  3946. if (exec->status < 0) {
  3947. testerr(exec);
  3948. }
  3949. #endif
  3950. return(exec->status);
  3951. }
  3952. /**
  3953. * xmlRegExecPushString:
  3954. * @exec: a regexp execution context or NULL to indicate the end
  3955. * @value: a string token input
  3956. * @data: data associated to the token to reuse in callbacks
  3957. *
  3958. * Push one input token in the execution context
  3959. *
  3960. * Returns: 1 if the regexp reached a final state, 0 if non-final, and
  3961. * a negative value in case of error.
  3962. */
  3963. int
  3964. xmlRegExecPushString(xmlRegExecCtxtPtr exec, const xmlChar *value,
  3965. void *data) {
  3966. return(xmlRegExecPushStringInternal(exec, value, data, 0));
  3967. }
  3968. /**
  3969. * xmlRegExecPushString2:
  3970. * @exec: a regexp execution context or NULL to indicate the end
  3971. * @value: the first string token input
  3972. * @value2: the second string token input
  3973. * @data: data associated to the token to reuse in callbacks
  3974. *
  3975. * Push one input token in the execution context
  3976. *
  3977. * Returns: 1 if the regexp reached a final state, 0 if non-final, and
  3978. * a negative value in case of error.
  3979. */
  3980. int
  3981. xmlRegExecPushString2(xmlRegExecCtxtPtr exec, const xmlChar *value,
  3982. const xmlChar *value2, void *data) {
  3983. xmlChar buf[150];
  3984. int lenn, lenp, ret;
  3985. xmlChar *str;
  3986. if (exec == NULL)
  3987. return(-1);
  3988. if (exec->comp == NULL)
  3989. return(-1);
  3990. if (exec->status != 0)
  3991. return(exec->status);
  3992. if (value2 == NULL)
  3993. return(xmlRegExecPushString(exec, value, data));
  3994. lenn = strlen((char *) value2);
  3995. lenp = strlen((char *) value);
  3996. if (150 < lenn + lenp + 2) {
  3997. str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
  3998. if (str == NULL) {
  3999. exec->status = -1;
  4000. return(-1);
  4001. }
  4002. } else {
  4003. str = buf;
  4004. }
  4005. memcpy(&str[0], value, lenp);
  4006. str[lenp] = XML_REG_STRING_SEPARATOR;
  4007. memcpy(&str[lenp + 1], value2, lenn);
  4008. str[lenn + lenp + 1] = 0;
  4009. if (exec->comp->compact != NULL)
  4010. ret = xmlRegCompactPushString(exec, exec->comp, str, data);
  4011. else
  4012. ret = xmlRegExecPushStringInternal(exec, str, data, 1);
  4013. if (str != buf)
  4014. xmlFree(str);
  4015. return(ret);
  4016. }
  4017. /**
  4018. * xmlRegExecGetValues:
  4019. * @exec: a regexp execution context
  4020. * @err: error extraction or normal one
  4021. * @nbval: pointer to the number of accepted values IN/OUT
  4022. * @nbneg: return number of negative transitions
  4023. * @values: pointer to the array of acceptable values
  4024. * @terminal: return value if this was a terminal state
  4025. *
  4026. * Extract information from the regexp execution, internal routine to
  4027. * implement xmlRegExecNextValues() and xmlRegExecErrInfo()
  4028. *
  4029. * Returns: 0 in case of success or -1 in case of error.
  4030. */
  4031. static int
  4032. xmlRegExecGetValues(xmlRegExecCtxtPtr exec, int err,
  4033. int *nbval, int *nbneg,
  4034. xmlChar **values, int *terminal) {
  4035. int maxval;
  4036. int nb = 0;
  4037. if ((exec == NULL) || (nbval == NULL) || (nbneg == NULL) ||
  4038. (values == NULL) || (*nbval <= 0))
  4039. return(-1);
  4040. maxval = *nbval;
  4041. *nbval = 0;
  4042. *nbneg = 0;
  4043. if ((exec->comp != NULL) && (exec->comp->compact != NULL)) {
  4044. xmlRegexpPtr comp;
  4045. int target, i, state;
  4046. comp = exec->comp;
  4047. if (err) {
  4048. if (exec->errStateNo == -1) return(-1);
  4049. state = exec->errStateNo;
  4050. } else {
  4051. state = exec->index;
  4052. }
  4053. if (terminal != NULL) {
  4054. if (comp->compact[state * (comp->nbstrings + 1)] ==
  4055. XML_REGEXP_FINAL_STATE)
  4056. *terminal = 1;
  4057. else
  4058. *terminal = 0;
  4059. }
  4060. for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) {
  4061. target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
  4062. if ((target > 0) && (target <= comp->nbstates) &&
  4063. (comp->compact[(target - 1) * (comp->nbstrings + 1)] !=
  4064. XML_REGEXP_SINK_STATE)) {
  4065. values[nb++] = comp->stringMap[i];
  4066. (*nbval)++;
  4067. }
  4068. }
  4069. for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) {
  4070. target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
  4071. if ((target > 0) && (target <= comp->nbstates) &&
  4072. (comp->compact[(target - 1) * (comp->nbstrings + 1)] ==
  4073. XML_REGEXP_SINK_STATE)) {
  4074. values[nb++] = comp->stringMap[i];
  4075. (*nbneg)++;
  4076. }
  4077. }
  4078. } else {
  4079. int transno;
  4080. xmlRegTransPtr trans;
  4081. xmlRegAtomPtr atom;
  4082. xmlRegStatePtr state;
  4083. if (terminal != NULL) {
  4084. if (exec->state->type == XML_REGEXP_FINAL_STATE)
  4085. *terminal = 1;
  4086. else
  4087. *terminal = 0;
  4088. }
  4089. if (err) {
  4090. if (exec->errState == NULL) return(-1);
  4091. state = exec->errState;
  4092. } else {
  4093. if (exec->state == NULL) return(-1);
  4094. state = exec->state;
  4095. }
  4096. for (transno = 0;
  4097. (transno < state->nbTrans) && (nb < maxval);
  4098. transno++) {
  4099. trans = &state->trans[transno];
  4100. if (trans->to < 0)
  4101. continue;
  4102. atom = trans->atom;
  4103. if ((atom == NULL) || (atom->valuep == NULL))
  4104. continue;
  4105. if (trans->count == REGEXP_ALL_LAX_COUNTER) {
  4106. /* this should not be reached but ... */
  4107. TODO;
  4108. } else if (trans->count == REGEXP_ALL_COUNTER) {
  4109. /* this should not be reached but ... */
  4110. TODO;
  4111. } else if (trans->counter >= 0) {
  4112. xmlRegCounterPtr counter = NULL;
  4113. int count;
  4114. if (err)
  4115. count = exec->errCounts[trans->counter];
  4116. else
  4117. count = exec->counts[trans->counter];
  4118. if (exec->comp != NULL)
  4119. counter = &exec->comp->counters[trans->counter];
  4120. if ((counter == NULL) || (count < counter->max)) {
  4121. if (atom->neg)
  4122. values[nb++] = (xmlChar *) atom->valuep2;
  4123. else
  4124. values[nb++] = (xmlChar *) atom->valuep;
  4125. (*nbval)++;
  4126. }
  4127. } else {
  4128. if ((exec->comp != NULL) && (exec->comp->states[trans->to] != NULL) &&
  4129. (exec->comp->states[trans->to]->type !=
  4130. XML_REGEXP_SINK_STATE)) {
  4131. if (atom->neg)
  4132. values[nb++] = (xmlChar *) atom->valuep2;
  4133. else
  4134. values[nb++] = (xmlChar *) atom->valuep;
  4135. (*nbval)++;
  4136. }
  4137. }
  4138. }
  4139. for (transno = 0;
  4140. (transno < state->nbTrans) && (nb < maxval);
  4141. transno++) {
  4142. trans = &state->trans[transno];
  4143. if (trans->to < 0)
  4144. continue;
  4145. atom = trans->atom;
  4146. if ((atom == NULL) || (atom->valuep == NULL))
  4147. continue;
  4148. if (trans->count == REGEXP_ALL_LAX_COUNTER) {
  4149. continue;
  4150. } else if (trans->count == REGEXP_ALL_COUNTER) {
  4151. continue;
  4152. } else if (trans->counter >= 0) {
  4153. continue;
  4154. } else {
  4155. if ((exec->comp->states[trans->to] != NULL) &&
  4156. (exec->comp->states[trans->to]->type ==
  4157. XML_REGEXP_SINK_STATE)) {
  4158. if (atom->neg)
  4159. values[nb++] = (xmlChar *) atom->valuep2;
  4160. else
  4161. values[nb++] = (xmlChar *) atom->valuep;
  4162. (*nbneg)++;
  4163. }
  4164. }
  4165. }
  4166. }
  4167. return(0);
  4168. }
  4169. /**
  4170. * xmlRegExecNextValues:
  4171. * @exec: a regexp execution context
  4172. * @nbval: pointer to the number of accepted values IN/OUT
  4173. * @nbneg: return number of negative transitions
  4174. * @values: pointer to the array of acceptable values
  4175. * @terminal: return value if this was a terminal state
  4176. *
  4177. * Extract information from the regexp execution,
  4178. * the parameter @values must point to an array of @nbval string pointers
  4179. * on return nbval will contain the number of possible strings in that
  4180. * state and the @values array will be updated with them. The string values
  4181. * returned will be freed with the @exec context and don't need to be
  4182. * deallocated.
  4183. *
  4184. * Returns: 0 in case of success or -1 in case of error.
  4185. */
  4186. int
  4187. xmlRegExecNextValues(xmlRegExecCtxtPtr exec, int *nbval, int *nbneg,
  4188. xmlChar **values, int *terminal) {
  4189. return(xmlRegExecGetValues(exec, 0, nbval, nbneg, values, terminal));
  4190. }
  4191. /**
  4192. * xmlRegExecErrInfo:
  4193. * @exec: a regexp execution context generating an error
  4194. * @string: return value for the error string
  4195. * @nbval: pointer to the number of accepted values IN/OUT
  4196. * @nbneg: return number of negative transitions
  4197. * @values: pointer to the array of acceptable values
  4198. * @terminal: return value if this was a terminal state
  4199. *
  4200. * Extract error information from the regexp execution, the parameter
  4201. * @string will be updated with the value pushed and not accepted,
  4202. * the parameter @values must point to an array of @nbval string pointers
  4203. * on return nbval will contain the number of possible strings in that
  4204. * state and the @values array will be updated with them. The string values
  4205. * returned will be freed with the @exec context and don't need to be
  4206. * deallocated.
  4207. *
  4208. * Returns: 0 in case of success or -1 in case of error.
  4209. */
  4210. int
  4211. xmlRegExecErrInfo(xmlRegExecCtxtPtr exec, const xmlChar **string,
  4212. int *nbval, int *nbneg, xmlChar **values, int *terminal) {
  4213. if (exec == NULL)
  4214. return(-1);
  4215. if (string != NULL) {
  4216. if (exec->status != 0)
  4217. *string = exec->errString;
  4218. else
  4219. *string = NULL;
  4220. }
  4221. return(xmlRegExecGetValues(exec, 1, nbval, nbneg, values, terminal));
  4222. }
  4223. #ifdef DEBUG_ERR
  4224. static void testerr(xmlRegExecCtxtPtr exec) {
  4225. const xmlChar *string;
  4226. xmlChar *values[5];
  4227. int nb = 5;
  4228. int nbneg;
  4229. int terminal;
  4230. xmlRegExecErrInfo(exec, &string, &nb, &nbneg, &values[0], &terminal);
  4231. }
  4232. #endif
  4233. #if 0
  4234. static int
  4235. xmlRegExecPushChar(xmlRegExecCtxtPtr exec, int UCS) {
  4236. xmlRegTransPtr trans;
  4237. xmlRegAtomPtr atom;
  4238. int ret;
  4239. int codepoint, len;
  4240. if (exec == NULL)
  4241. return(-1);
  4242. if (exec->status != 0)
  4243. return(exec->status);
  4244. while ((exec->status == 0) &&
  4245. ((exec->inputString[exec->index] != 0) ||
  4246. (exec->state->type != XML_REGEXP_FINAL_STATE))) {
  4247. /*
  4248. * End of input on non-terminal state, rollback, however we may
  4249. * still have epsilon like transition for counted transitions
  4250. * on counters, in that case don't break too early.
  4251. */
  4252. if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL))
  4253. goto rollback;
  4254. exec->transcount = 0;
  4255. for (;exec->transno < exec->state->nbTrans;exec->transno++) {
  4256. trans = &exec->state->trans[exec->transno];
  4257. if (trans->to < 0)
  4258. continue;
  4259. atom = trans->atom;
  4260. ret = 0;
  4261. if (trans->count >= 0) {
  4262. int count;
  4263. xmlRegCounterPtr counter;
  4264. /*
  4265. * A counted transition.
  4266. */
  4267. count = exec->counts[trans->count];
  4268. counter = &exec->comp->counters[trans->count];
  4269. #ifdef DEBUG_REGEXP_EXEC
  4270. printf("testing count %d: val %d, min %d, max %d\n",
  4271. trans->count, count, counter->min, counter->max);
  4272. #endif
  4273. ret = ((count >= counter->min) && (count <= counter->max));
  4274. } else if (atom == NULL) {
  4275. fprintf(stderr, "epsilon transition left at runtime\n");
  4276. exec->status = -2;
  4277. break;
  4278. } else if (exec->inputString[exec->index] != 0) {
  4279. codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
  4280. ret = xmlRegCheckCharacter(atom, codepoint);
  4281. if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
  4282. xmlRegStatePtr to = exec->comp->states[trans->to];
  4283. /*
  4284. * this is a multiple input sequence
  4285. */
  4286. if (exec->state->nbTrans > exec->transno + 1) {
  4287. xmlFARegExecSave(exec);
  4288. }
  4289. exec->transcount = 1;
  4290. do {
  4291. /*
  4292. * Try to progress as much as possible on the input
  4293. */
  4294. if (exec->transcount == atom->max) {
  4295. break;
  4296. }
  4297. exec->index += len;
  4298. /*
  4299. * End of input: stop here
  4300. */
  4301. if (exec->inputString[exec->index] == 0) {
  4302. exec->index -= len;
  4303. break;
  4304. }
  4305. if (exec->transcount >= atom->min) {
  4306. int transno = exec->transno;
  4307. xmlRegStatePtr state = exec->state;
  4308. /*
  4309. * The transition is acceptable save it
  4310. */
  4311. exec->transno = -1; /* trick */
  4312. exec->state = to;
  4313. xmlFARegExecSave(exec);
  4314. exec->transno = transno;
  4315. exec->state = state;
  4316. }
  4317. codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
  4318. len);
  4319. ret = xmlRegCheckCharacter(atom, codepoint);
  4320. exec->transcount++;
  4321. } while (ret == 1);
  4322. if (exec->transcount < atom->min)
  4323. ret = 0;
  4324. /*
  4325. * If the last check failed but one transition was found
  4326. * possible, rollback
  4327. */
  4328. if (ret < 0)
  4329. ret = 0;
  4330. if (ret == 0) {
  4331. goto rollback;
  4332. }
  4333. }
  4334. }
  4335. if (ret == 1) {
  4336. if (exec->state->nbTrans > exec->transno + 1) {
  4337. xmlFARegExecSave(exec);
  4338. }
  4339. /*
  4340. * restart count for expressions like this ((abc){2})*
  4341. */
  4342. if (trans->count >= 0) {
  4343. #ifdef DEBUG_REGEXP_EXEC
  4344. printf("Reset count %d\n", trans->count);
  4345. #endif
  4346. exec->counts[trans->count] = 0;
  4347. }
  4348. if (trans->counter >= 0) {
  4349. #ifdef DEBUG_REGEXP_EXEC
  4350. printf("Increasing count %d\n", trans->counter);
  4351. #endif
  4352. exec->counts[trans->counter]++;
  4353. }
  4354. #ifdef DEBUG_REGEXP_EXEC
  4355. printf("entering state %d\n", trans->to);
  4356. #endif
  4357. exec->state = exec->comp->states[trans->to];
  4358. exec->transno = 0;
  4359. if (trans->atom != NULL) {
  4360. exec->index += len;
  4361. }
  4362. goto progress;
  4363. } else if (ret < 0) {
  4364. exec->status = -4;
  4365. break;
  4366. }
  4367. }
  4368. if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
  4369. rollback:
  4370. /*
  4371. * Failed to find a way out
  4372. */
  4373. exec->determinist = 0;
  4374. xmlFARegExecRollBack(exec);
  4375. }
  4376. progress:
  4377. continue;
  4378. }
  4379. }
  4380. #endif
  4381. /************************************************************************
  4382. * *
  4383. * Parser for the Schemas Datatype Regular Expressions *
  4384. * http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#regexs *
  4385. * *
  4386. ************************************************************************/
  4387. /**
  4388. * xmlFAIsChar:
  4389. * @ctxt: a regexp parser context
  4390. *
  4391. * [10] Char ::= [^.\?*+()|#x5B#x5D]
  4392. */
  4393. static int
  4394. xmlFAIsChar(xmlRegParserCtxtPtr ctxt) {
  4395. int cur;
  4396. int len;
  4397. cur = CUR_SCHAR(ctxt->cur, len);
  4398. if ((cur == '.') || (cur == '\\') || (cur == '?') ||
  4399. (cur == '*') || (cur == '+') || (cur == '(') ||
  4400. (cur == ')') || (cur == '|') || (cur == 0x5B) ||
  4401. (cur == 0x5D) || (cur == 0))
  4402. return(-1);
  4403. return(cur);
  4404. }
  4405. /**
  4406. * xmlFAParseCharProp:
  4407. * @ctxt: a regexp parser context
  4408. *
  4409. * [27] charProp ::= IsCategory | IsBlock
  4410. * [28] IsCategory ::= Letters | Marks | Numbers | Punctuation |
  4411. * Separators | Symbols | Others
  4412. * [29] Letters ::= 'L' [ultmo]?
  4413. * [30] Marks ::= 'M' [nce]?
  4414. * [31] Numbers ::= 'N' [dlo]?
  4415. * [32] Punctuation ::= 'P' [cdseifo]?
  4416. * [33] Separators ::= 'Z' [slp]?
  4417. * [34] Symbols ::= 'S' [mcko]?
  4418. * [35] Others ::= 'C' [cfon]?
  4419. * [36] IsBlock ::= 'Is' [a-zA-Z0-9#x2D]+
  4420. */
  4421. static void
  4422. xmlFAParseCharProp(xmlRegParserCtxtPtr ctxt) {
  4423. int cur;
  4424. xmlRegAtomType type = (xmlRegAtomType) 0;
  4425. xmlChar *blockName = NULL;
  4426. cur = CUR;
  4427. if (cur == 'L') {
  4428. NEXT;
  4429. cur = CUR;
  4430. if (cur == 'u') {
  4431. NEXT;
  4432. type = XML_REGEXP_LETTER_UPPERCASE;
  4433. } else if (cur == 'l') {
  4434. NEXT;
  4435. type = XML_REGEXP_LETTER_LOWERCASE;
  4436. } else if (cur == 't') {
  4437. NEXT;
  4438. type = XML_REGEXP_LETTER_TITLECASE;
  4439. } else if (cur == 'm') {
  4440. NEXT;
  4441. type = XML_REGEXP_LETTER_MODIFIER;
  4442. } else if (cur == 'o') {
  4443. NEXT;
  4444. type = XML_REGEXP_LETTER_OTHERS;
  4445. } else {
  4446. type = XML_REGEXP_LETTER;
  4447. }
  4448. } else if (cur == 'M') {
  4449. NEXT;
  4450. cur = CUR;
  4451. if (cur == 'n') {
  4452. NEXT;
  4453. /* nonspacing */
  4454. type = XML_REGEXP_MARK_NONSPACING;
  4455. } else if (cur == 'c') {
  4456. NEXT;
  4457. /* spacing combining */
  4458. type = XML_REGEXP_MARK_SPACECOMBINING;
  4459. } else if (cur == 'e') {
  4460. NEXT;
  4461. /* enclosing */
  4462. type = XML_REGEXP_MARK_ENCLOSING;
  4463. } else {
  4464. /* all marks */
  4465. type = XML_REGEXP_MARK;
  4466. }
  4467. } else if (cur == 'N') {
  4468. NEXT;
  4469. cur = CUR;
  4470. if (cur == 'd') {
  4471. NEXT;
  4472. /* digital */
  4473. type = XML_REGEXP_NUMBER_DECIMAL;
  4474. } else if (cur == 'l') {
  4475. NEXT;
  4476. /* letter */
  4477. type = XML_REGEXP_NUMBER_LETTER;
  4478. } else if (cur == 'o') {
  4479. NEXT;
  4480. /* other */
  4481. type = XML_REGEXP_NUMBER_OTHERS;
  4482. } else {
  4483. /* all numbers */
  4484. type = XML_REGEXP_NUMBER;
  4485. }
  4486. } else if (cur == 'P') {
  4487. NEXT;
  4488. cur = CUR;
  4489. if (cur == 'c') {
  4490. NEXT;
  4491. /* connector */
  4492. type = XML_REGEXP_PUNCT_CONNECTOR;
  4493. } else if (cur == 'd') {
  4494. NEXT;
  4495. /* dash */
  4496. type = XML_REGEXP_PUNCT_DASH;
  4497. } else if (cur == 's') {
  4498. NEXT;
  4499. /* open */
  4500. type = XML_REGEXP_PUNCT_OPEN;
  4501. } else if (cur == 'e') {
  4502. NEXT;
  4503. /* close */
  4504. type = XML_REGEXP_PUNCT_CLOSE;
  4505. } else if (cur == 'i') {
  4506. NEXT;
  4507. /* initial quote */
  4508. type = XML_REGEXP_PUNCT_INITQUOTE;
  4509. } else if (cur == 'f') {
  4510. NEXT;
  4511. /* final quote */
  4512. type = XML_REGEXP_PUNCT_FINQUOTE;
  4513. } else if (cur == 'o') {
  4514. NEXT;
  4515. /* other */
  4516. type = XML_REGEXP_PUNCT_OTHERS;
  4517. } else {
  4518. /* all punctuation */
  4519. type = XML_REGEXP_PUNCT;
  4520. }
  4521. } else if (cur == 'Z') {
  4522. NEXT;
  4523. cur = CUR;
  4524. if (cur == 's') {
  4525. NEXT;
  4526. /* space */
  4527. type = XML_REGEXP_SEPAR_SPACE;
  4528. } else if (cur == 'l') {
  4529. NEXT;
  4530. /* line */
  4531. type = XML_REGEXP_SEPAR_LINE;
  4532. } else if (cur == 'p') {
  4533. NEXT;
  4534. /* paragraph */
  4535. type = XML_REGEXP_SEPAR_PARA;
  4536. } else {
  4537. /* all separators */
  4538. type = XML_REGEXP_SEPAR;
  4539. }
  4540. } else if (cur == 'S') {
  4541. NEXT;
  4542. cur = CUR;
  4543. if (cur == 'm') {
  4544. NEXT;
  4545. type = XML_REGEXP_SYMBOL_MATH;
  4546. /* math */
  4547. } else if (cur == 'c') {
  4548. NEXT;
  4549. type = XML_REGEXP_SYMBOL_CURRENCY;
  4550. /* currency */
  4551. } else if (cur == 'k') {
  4552. NEXT;
  4553. type = XML_REGEXP_SYMBOL_MODIFIER;
  4554. /* modifiers */
  4555. } else if (cur == 'o') {
  4556. NEXT;
  4557. type = XML_REGEXP_SYMBOL_OTHERS;
  4558. /* other */
  4559. } else {
  4560. /* all symbols */
  4561. type = XML_REGEXP_SYMBOL;
  4562. }
  4563. } else if (cur == 'C') {
  4564. NEXT;
  4565. cur = CUR;
  4566. if (cur == 'c') {
  4567. NEXT;
  4568. /* control */
  4569. type = XML_REGEXP_OTHER_CONTROL;
  4570. } else if (cur == 'f') {
  4571. NEXT;
  4572. /* format */
  4573. type = XML_REGEXP_OTHER_FORMAT;
  4574. } else if (cur == 'o') {
  4575. NEXT;
  4576. /* private use */
  4577. type = XML_REGEXP_OTHER_PRIVATE;
  4578. } else if (cur == 'n') {
  4579. NEXT;
  4580. /* not assigned */
  4581. type = XML_REGEXP_OTHER_NA;
  4582. } else {
  4583. /* all others */
  4584. type = XML_REGEXP_OTHER;
  4585. }
  4586. } else if (cur == 'I') {
  4587. const xmlChar *start;
  4588. NEXT;
  4589. cur = CUR;
  4590. if (cur != 's') {
  4591. ERROR("IsXXXX expected");
  4592. return;
  4593. }
  4594. NEXT;
  4595. start = ctxt->cur;
  4596. cur = CUR;
  4597. if (((cur >= 'a') && (cur <= 'z')) ||
  4598. ((cur >= 'A') && (cur <= 'Z')) ||
  4599. ((cur >= '0') && (cur <= '9')) ||
  4600. (cur == 0x2D)) {
  4601. NEXT;
  4602. cur = CUR;
  4603. while (((cur >= 'a') && (cur <= 'z')) ||
  4604. ((cur >= 'A') && (cur <= 'Z')) ||
  4605. ((cur >= '0') && (cur <= '9')) ||
  4606. (cur == 0x2D)) {
  4607. NEXT;
  4608. cur = CUR;
  4609. }
  4610. }
  4611. type = XML_REGEXP_BLOCK_NAME;
  4612. blockName = xmlStrndup(start, ctxt->cur - start);
  4613. } else {
  4614. ERROR("Unknown char property");
  4615. return;
  4616. }
  4617. if (ctxt->atom == NULL) {
  4618. ctxt->atom = xmlRegNewAtom(ctxt, type);
  4619. if (ctxt->atom != NULL)
  4620. ctxt->atom->valuep = blockName;
  4621. } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
  4622. xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
  4623. type, 0, 0, blockName);
  4624. }
  4625. }
  4626. /**
  4627. * xmlFAParseCharClassEsc:
  4628. * @ctxt: a regexp parser context
  4629. *
  4630. * [23] charClassEsc ::= ( SingleCharEsc | MultiCharEsc | catEsc | complEsc )
  4631. * [24] SingleCharEsc ::= '\' [nrt\|.?*+(){}#x2D#x5B#x5D#x5E]
  4632. * [25] catEsc ::= '\p{' charProp '}'
  4633. * [26] complEsc ::= '\P{' charProp '}'
  4634. * [37] MultiCharEsc ::= '.' | ('\' [sSiIcCdDwW])
  4635. */
  4636. static void
  4637. xmlFAParseCharClassEsc(xmlRegParserCtxtPtr ctxt) {
  4638. int cur;
  4639. if (CUR == '.') {
  4640. if (ctxt->atom == NULL) {
  4641. ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_ANYCHAR);
  4642. } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
  4643. xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
  4644. XML_REGEXP_ANYCHAR, 0, 0, NULL);
  4645. }
  4646. NEXT;
  4647. return;
  4648. }
  4649. if (CUR != '\\') {
  4650. ERROR("Escaped sequence: expecting \\");
  4651. return;
  4652. }
  4653. NEXT;
  4654. cur = CUR;
  4655. if (cur == 'p') {
  4656. NEXT;
  4657. if (CUR != '{') {
  4658. ERROR("Expecting '{'");
  4659. return;
  4660. }
  4661. NEXT;
  4662. xmlFAParseCharProp(ctxt);
  4663. if (CUR != '}') {
  4664. ERROR("Expecting '}'");
  4665. return;
  4666. }
  4667. NEXT;
  4668. } else if (cur == 'P') {
  4669. NEXT;
  4670. if (CUR != '{') {
  4671. ERROR("Expecting '{'");
  4672. return;
  4673. }
  4674. NEXT;
  4675. xmlFAParseCharProp(ctxt);
  4676. if (ctxt->atom != NULL)
  4677. ctxt->atom->neg = 1;
  4678. if (CUR != '}') {
  4679. ERROR("Expecting '}'");
  4680. return;
  4681. }
  4682. NEXT;
  4683. } else if ((cur == 'n') || (cur == 'r') || (cur == 't') || (cur == '\\') ||
  4684. (cur == '|') || (cur == '.') || (cur == '?') || (cur == '*') ||
  4685. (cur == '+') || (cur == '(') || (cur == ')') || (cur == '{') ||
  4686. (cur == '}') || (cur == 0x2D) || (cur == 0x5B) || (cur == 0x5D) ||
  4687. (cur == 0x5E)) {
  4688. if (ctxt->atom == NULL) {
  4689. ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
  4690. if (ctxt->atom != NULL) {
  4691. switch (cur) {
  4692. case 'n':
  4693. ctxt->atom->codepoint = '\n';
  4694. break;
  4695. case 'r':
  4696. ctxt->atom->codepoint = '\r';
  4697. break;
  4698. case 't':
  4699. ctxt->atom->codepoint = '\t';
  4700. break;
  4701. default:
  4702. ctxt->atom->codepoint = cur;
  4703. }
  4704. }
  4705. } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
  4706. switch (cur) {
  4707. case 'n':
  4708. cur = '\n';
  4709. break;
  4710. case 'r':
  4711. cur = '\r';
  4712. break;
  4713. case 't':
  4714. cur = '\t';
  4715. break;
  4716. }
  4717. xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
  4718. XML_REGEXP_CHARVAL, cur, cur, NULL);
  4719. }
  4720. NEXT;
  4721. } else if ((cur == 's') || (cur == 'S') || (cur == 'i') || (cur == 'I') ||
  4722. (cur == 'c') || (cur == 'C') || (cur == 'd') || (cur == 'D') ||
  4723. (cur == 'w') || (cur == 'W')) {
  4724. xmlRegAtomType type = XML_REGEXP_ANYSPACE;
  4725. switch (cur) {
  4726. case 's':
  4727. type = XML_REGEXP_ANYSPACE;
  4728. break;
  4729. case 'S':
  4730. type = XML_REGEXP_NOTSPACE;
  4731. break;
  4732. case 'i':
  4733. type = XML_REGEXP_INITNAME;
  4734. break;
  4735. case 'I':
  4736. type = XML_REGEXP_NOTINITNAME;
  4737. break;
  4738. case 'c':
  4739. type = XML_REGEXP_NAMECHAR;
  4740. break;
  4741. case 'C':
  4742. type = XML_REGEXP_NOTNAMECHAR;
  4743. break;
  4744. case 'd':
  4745. type = XML_REGEXP_DECIMAL;
  4746. break;
  4747. case 'D':
  4748. type = XML_REGEXP_NOTDECIMAL;
  4749. break;
  4750. case 'w':
  4751. type = XML_REGEXP_REALCHAR;
  4752. break;
  4753. case 'W':
  4754. type = XML_REGEXP_NOTREALCHAR;
  4755. break;
  4756. }
  4757. NEXT;
  4758. if (ctxt->atom == NULL) {
  4759. ctxt->atom = xmlRegNewAtom(ctxt, type);
  4760. } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
  4761. xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
  4762. type, 0, 0, NULL);
  4763. }
  4764. } else {
  4765. ERROR("Wrong escape sequence, misuse of character '\\'");
  4766. }
  4767. }
  4768. /**
  4769. * xmlFAParseCharRange:
  4770. * @ctxt: a regexp parser context
  4771. *
  4772. * [17] charRange ::= seRange | XmlCharRef | XmlCharIncDash
  4773. * [18] seRange ::= charOrEsc '-' charOrEsc
  4774. * [20] charOrEsc ::= XmlChar | SingleCharEsc
  4775. * [21] XmlChar ::= [^\#x2D#x5B#x5D]
  4776. * [22] XmlCharIncDash ::= [^\#x5B#x5D]
  4777. */
  4778. static void
  4779. xmlFAParseCharRange(xmlRegParserCtxtPtr ctxt) {
  4780. int cur, len;
  4781. int start = -1;
  4782. int end = -1;
  4783. if (CUR == '\0') {
  4784. ERROR("Expecting ']'");
  4785. return;
  4786. }
  4787. cur = CUR;
  4788. if (cur == '\\') {
  4789. NEXT;
  4790. cur = CUR;
  4791. switch (cur) {
  4792. case 'n': start = 0xA; break;
  4793. case 'r': start = 0xD; break;
  4794. case 't': start = 0x9; break;
  4795. case '\\': case '|': case '.': case '-': case '^': case '?':
  4796. case '*': case '+': case '{': case '}': case '(': case ')':
  4797. case '[': case ']':
  4798. start = cur; break;
  4799. default:
  4800. ERROR("Invalid escape value");
  4801. return;
  4802. }
  4803. end = start;
  4804. len = 1;
  4805. } else if ((cur != 0x5B) && (cur != 0x5D)) {
  4806. end = start = CUR_SCHAR(ctxt->cur, len);
  4807. } else {
  4808. ERROR("Expecting a char range");
  4809. return;
  4810. }
  4811. /*
  4812. * Since we are "inside" a range, we can assume ctxt->cur is past
  4813. * the start of ctxt->string, and PREV should be safe
  4814. */
  4815. if ((start == '-') && (NXT(1) != ']') && (PREV != '[') && (PREV != '^')) {
  4816. NEXTL(len);
  4817. return;
  4818. }
  4819. NEXTL(len);
  4820. cur = CUR;
  4821. if ((cur != '-') || (NXT(1) == ']')) {
  4822. xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
  4823. XML_REGEXP_CHARVAL, start, end, NULL);
  4824. return;
  4825. }
  4826. NEXT;
  4827. cur = CUR;
  4828. if (cur == '\\') {
  4829. NEXT;
  4830. cur = CUR;
  4831. switch (cur) {
  4832. case 'n': end = 0xA; break;
  4833. case 'r': end = 0xD; break;
  4834. case 't': end = 0x9; break;
  4835. case '\\': case '|': case '.': case '-': case '^': case '?':
  4836. case '*': case '+': case '{': case '}': case '(': case ')':
  4837. case '[': case ']':
  4838. end = cur; break;
  4839. default:
  4840. ERROR("Invalid escape value");
  4841. return;
  4842. }
  4843. len = 1;
  4844. } else if ((cur != '\0') && (cur != 0x5B) && (cur != 0x5D)) {
  4845. end = CUR_SCHAR(ctxt->cur, len);
  4846. } else {
  4847. ERROR("Expecting the end of a char range");
  4848. return;
  4849. }
  4850. /* TODO check that the values are acceptable character ranges for XML */
  4851. if (end < start) {
  4852. ERROR("End of range is before start of range");
  4853. } else {
  4854. NEXTL(len);
  4855. xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
  4856. XML_REGEXP_CHARVAL, start, end, NULL);
  4857. }
  4858. return;
  4859. }
  4860. /**
  4861. * xmlFAParsePosCharGroup:
  4862. * @ctxt: a regexp parser context
  4863. *
  4864. * [14] posCharGroup ::= ( charRange | charClassEsc )+
  4865. */
  4866. static void
  4867. xmlFAParsePosCharGroup(xmlRegParserCtxtPtr ctxt) {
  4868. do {
  4869. if (CUR == '\\') {
  4870. xmlFAParseCharClassEsc(ctxt);
  4871. } else {
  4872. xmlFAParseCharRange(ctxt);
  4873. }
  4874. } while ((CUR != ']') && (CUR != '-') &&
  4875. (CUR != 0) && (ctxt->error == 0));
  4876. }
  4877. /**
  4878. * xmlFAParseCharGroup:
  4879. * @ctxt: a regexp parser context
  4880. *
  4881. * [13] charGroup ::= posCharGroup | negCharGroup | charClassSub
  4882. * [15] negCharGroup ::= '^' posCharGroup
  4883. * [16] charClassSub ::= ( posCharGroup | negCharGroup ) '-' charClassExpr
  4884. * [12] charClassExpr ::= '[' charGroup ']'
  4885. */
  4886. static void
  4887. xmlFAParseCharGroup(xmlRegParserCtxtPtr ctxt) {
  4888. int neg = ctxt->neg;
  4889. if (CUR == '^') {
  4890. NEXT;
  4891. ctxt->neg = !ctxt->neg;
  4892. xmlFAParsePosCharGroup(ctxt);
  4893. ctxt->neg = neg;
  4894. }
  4895. while ((CUR != ']') && (ctxt->error == 0)) {
  4896. if ((CUR == '-') && (NXT(1) == '[')) {
  4897. NEXT; /* eat the '-' */
  4898. NEXT; /* eat the '[' */
  4899. ctxt->neg = 2;
  4900. xmlFAParseCharGroup(ctxt);
  4901. ctxt->neg = neg;
  4902. if (CUR == ']') {
  4903. NEXT;
  4904. } else {
  4905. ERROR("charClassExpr: ']' expected");
  4906. }
  4907. break;
  4908. } else {
  4909. xmlFAParsePosCharGroup(ctxt);
  4910. }
  4911. }
  4912. }
  4913. /**
  4914. * xmlFAParseCharClass:
  4915. * @ctxt: a regexp parser context
  4916. *
  4917. * [11] charClass ::= charClassEsc | charClassExpr
  4918. * [12] charClassExpr ::= '[' charGroup ']'
  4919. */
  4920. static void
  4921. xmlFAParseCharClass(xmlRegParserCtxtPtr ctxt) {
  4922. if (CUR == '[') {
  4923. NEXT;
  4924. ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_RANGES);
  4925. if (ctxt->atom == NULL)
  4926. return;
  4927. xmlFAParseCharGroup(ctxt);
  4928. if (CUR == ']') {
  4929. NEXT;
  4930. } else {
  4931. ERROR("xmlFAParseCharClass: ']' expected");
  4932. }
  4933. } else {
  4934. xmlFAParseCharClassEsc(ctxt);
  4935. }
  4936. }
  4937. /**
  4938. * xmlFAParseQuantExact:
  4939. * @ctxt: a regexp parser context
  4940. *
  4941. * [8] QuantExact ::= [0-9]+
  4942. *
  4943. * Returns 0 if success or -1 in case of error
  4944. */
  4945. static int
  4946. xmlFAParseQuantExact(xmlRegParserCtxtPtr ctxt) {
  4947. int ret = 0;
  4948. int ok = 0;
  4949. int overflow = 0;
  4950. while ((CUR >= '0') && (CUR <= '9')) {
  4951. if (ret > INT_MAX / 10) {
  4952. overflow = 1;
  4953. } else {
  4954. int digit = CUR - '0';
  4955. ret *= 10;
  4956. if (ret > INT_MAX - digit)
  4957. overflow = 1;
  4958. else
  4959. ret += digit;
  4960. }
  4961. ok = 1;
  4962. NEXT;
  4963. }
  4964. if ((ok != 1) || (overflow == 1)) {
  4965. return(-1);
  4966. }
  4967. return(ret);
  4968. }
  4969. /**
  4970. * xmlFAParseQuantifier:
  4971. * @ctxt: a regexp parser context
  4972. *
  4973. * [4] quantifier ::= [?*+] | ( '{' quantity '}' )
  4974. * [5] quantity ::= quantRange | quantMin | QuantExact
  4975. * [6] quantRange ::= QuantExact ',' QuantExact
  4976. * [7] quantMin ::= QuantExact ','
  4977. * [8] QuantExact ::= [0-9]+
  4978. */
  4979. static int
  4980. xmlFAParseQuantifier(xmlRegParserCtxtPtr ctxt) {
  4981. int cur;
  4982. cur = CUR;
  4983. if ((cur == '?') || (cur == '*') || (cur == '+')) {
  4984. if (ctxt->atom != NULL) {
  4985. if (cur == '?')
  4986. ctxt->atom->quant = XML_REGEXP_QUANT_OPT;
  4987. else if (cur == '*')
  4988. ctxt->atom->quant = XML_REGEXP_QUANT_MULT;
  4989. else if (cur == '+')
  4990. ctxt->atom->quant = XML_REGEXP_QUANT_PLUS;
  4991. }
  4992. NEXT;
  4993. return(1);
  4994. }
  4995. if (cur == '{') {
  4996. int min = 0, max = 0;
  4997. NEXT;
  4998. cur = xmlFAParseQuantExact(ctxt);
  4999. if (cur >= 0)
  5000. min = cur;
  5001. else {
  5002. ERROR("Improper quantifier");
  5003. }
  5004. if (CUR == ',') {
  5005. NEXT;
  5006. if (CUR == '}')
  5007. max = INT_MAX;
  5008. else {
  5009. cur = xmlFAParseQuantExact(ctxt);
  5010. if (cur >= 0)
  5011. max = cur;
  5012. else {
  5013. ERROR("Improper quantifier");
  5014. }
  5015. }
  5016. }
  5017. if (CUR == '}') {
  5018. NEXT;
  5019. } else {
  5020. ERROR("Unterminated quantifier");
  5021. }
  5022. if (max == 0)
  5023. max = min;
  5024. if (ctxt->atom != NULL) {
  5025. ctxt->atom->quant = XML_REGEXP_QUANT_RANGE;
  5026. ctxt->atom->min = min;
  5027. ctxt->atom->max = max;
  5028. }
  5029. return(1);
  5030. }
  5031. return(0);
  5032. }
  5033. /**
  5034. * xmlFAParseAtom:
  5035. * @ctxt: a regexp parser context
  5036. *
  5037. * [9] atom ::= Char | charClass | ( '(' regExp ')' )
  5038. */
  5039. static int
  5040. xmlFAParseAtom(xmlRegParserCtxtPtr ctxt) {
  5041. int codepoint, len;
  5042. codepoint = xmlFAIsChar(ctxt);
  5043. if (codepoint > 0) {
  5044. ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
  5045. if (ctxt->atom == NULL)
  5046. return(-1);
  5047. codepoint = CUR_SCHAR(ctxt->cur, len);
  5048. ctxt->atom->codepoint = codepoint;
  5049. NEXTL(len);
  5050. return(1);
  5051. } else if (CUR == '|') {
  5052. return(0);
  5053. } else if (CUR == 0) {
  5054. return(0);
  5055. } else if (CUR == ')') {
  5056. return(0);
  5057. } else if (CUR == '(') {
  5058. xmlRegStatePtr start, oldend, start0;
  5059. NEXT;
  5060. if (ctxt->depth >= 50) {
  5061. ERROR("xmlFAParseAtom: maximum nesting depth exceeded");
  5062. return(-1);
  5063. }
  5064. /*
  5065. * this extra Epsilon transition is needed if we count with 0 allowed
  5066. * unfortunately this can't be known at that point
  5067. */
  5068. xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
  5069. start0 = ctxt->state;
  5070. xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
  5071. start = ctxt->state;
  5072. oldend = ctxt->end;
  5073. ctxt->end = NULL;
  5074. ctxt->atom = NULL;
  5075. ctxt->depth++;
  5076. xmlFAParseRegExp(ctxt, 0);
  5077. ctxt->depth--;
  5078. if (CUR == ')') {
  5079. NEXT;
  5080. } else {
  5081. ERROR("xmlFAParseAtom: expecting ')'");
  5082. }
  5083. ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_SUBREG);
  5084. if (ctxt->atom == NULL)
  5085. return(-1);
  5086. ctxt->atom->start = start;
  5087. ctxt->atom->start0 = start0;
  5088. ctxt->atom->stop = ctxt->state;
  5089. ctxt->end = oldend;
  5090. return(1);
  5091. } else if ((CUR == '[') || (CUR == '\\') || (CUR == '.')) {
  5092. xmlFAParseCharClass(ctxt);
  5093. return(1);
  5094. }
  5095. return(0);
  5096. }
  5097. /**
  5098. * xmlFAParsePiece:
  5099. * @ctxt: a regexp parser context
  5100. *
  5101. * [3] piece ::= atom quantifier?
  5102. */
  5103. static int
  5104. xmlFAParsePiece(xmlRegParserCtxtPtr ctxt) {
  5105. int ret;
  5106. ctxt->atom = NULL;
  5107. ret = xmlFAParseAtom(ctxt);
  5108. if (ret == 0)
  5109. return(0);
  5110. if (ctxt->atom == NULL) {
  5111. ERROR("internal: no atom generated");
  5112. }
  5113. xmlFAParseQuantifier(ctxt);
  5114. return(1);
  5115. }
  5116. /**
  5117. * xmlFAParseBranch:
  5118. * @ctxt: a regexp parser context
  5119. * @to: optional target to the end of the branch
  5120. *
  5121. * @to is used to optimize by removing duplicate path in automata
  5122. * in expressions like (a|b)(c|d)
  5123. *
  5124. * [2] branch ::= piece*
  5125. */
  5126. static int
  5127. xmlFAParseBranch(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr to) {
  5128. xmlRegStatePtr previous;
  5129. int ret;
  5130. previous = ctxt->state;
  5131. ret = xmlFAParsePiece(ctxt);
  5132. if (ret == 0) {
  5133. /* Empty branch */
  5134. xmlFAGenerateEpsilonTransition(ctxt, previous, to);
  5135. } else {
  5136. if (xmlFAGenerateTransitions(ctxt, previous,
  5137. (CUR=='|' || CUR==')' || CUR==0) ? to : NULL, ctxt->atom) < 0)
  5138. return(-1);
  5139. previous = ctxt->state;
  5140. ctxt->atom = NULL;
  5141. }
  5142. while ((ret != 0) && (ctxt->error == 0)) {
  5143. ret = xmlFAParsePiece(ctxt);
  5144. if (ret != 0) {
  5145. if (xmlFAGenerateTransitions(ctxt, previous,
  5146. (CUR=='|' || CUR==')' || CUR==0) ? to : NULL,
  5147. ctxt->atom) < 0)
  5148. return(-1);
  5149. previous = ctxt->state;
  5150. ctxt->atom = NULL;
  5151. }
  5152. }
  5153. return(0);
  5154. }
  5155. /**
  5156. * xmlFAParseRegExp:
  5157. * @ctxt: a regexp parser context
  5158. * @top: is this the top-level expression ?
  5159. *
  5160. * [1] regExp ::= branch ( '|' branch )*
  5161. */
  5162. static void
  5163. xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top) {
  5164. xmlRegStatePtr start, end;
  5165. /* if not top start should have been generated by an epsilon trans */
  5166. start = ctxt->state;
  5167. ctxt->end = NULL;
  5168. xmlFAParseBranch(ctxt, NULL);
  5169. if (top) {
  5170. #ifdef DEBUG_REGEXP_GRAPH
  5171. printf("State %d is final\n", ctxt->state->no);
  5172. #endif
  5173. ctxt->state->type = XML_REGEXP_FINAL_STATE;
  5174. }
  5175. if (CUR != '|') {
  5176. ctxt->end = ctxt->state;
  5177. return;
  5178. }
  5179. end = ctxt->state;
  5180. while ((CUR == '|') && (ctxt->error == 0)) {
  5181. NEXT;
  5182. ctxt->state = start;
  5183. ctxt->end = NULL;
  5184. xmlFAParseBranch(ctxt, end);
  5185. }
  5186. if (!top) {
  5187. ctxt->state = end;
  5188. ctxt->end = end;
  5189. }
  5190. }
  5191. /************************************************************************
  5192. * *
  5193. * The basic API *
  5194. * *
  5195. ************************************************************************/
  5196. /**
  5197. * xmlRegexpPrint:
  5198. * @output: the file for the output debug
  5199. * @regexp: the compiled regexp
  5200. *
  5201. * Print the content of the compiled regular expression
  5202. */
  5203. void
  5204. xmlRegexpPrint(FILE *output, xmlRegexpPtr regexp) {
  5205. int i;
  5206. if (output == NULL)
  5207. return;
  5208. fprintf(output, " regexp: ");
  5209. if (regexp == NULL) {
  5210. fprintf(output, "NULL\n");
  5211. return;
  5212. }
  5213. fprintf(output, "'%s' ", regexp->string);
  5214. fprintf(output, "\n");
  5215. fprintf(output, "%d atoms:\n", regexp->nbAtoms);
  5216. for (i = 0;i < regexp->nbAtoms; i++) {
  5217. fprintf(output, " %02d ", i);
  5218. xmlRegPrintAtom(output, regexp->atoms[i]);
  5219. }
  5220. fprintf(output, "%d states:", regexp->nbStates);
  5221. fprintf(output, "\n");
  5222. for (i = 0;i < regexp->nbStates; i++) {
  5223. xmlRegPrintState(output, regexp->states[i]);
  5224. }
  5225. fprintf(output, "%d counters:\n", regexp->nbCounters);
  5226. for (i = 0;i < regexp->nbCounters; i++) {
  5227. fprintf(output, " %d: min %d max %d\n", i, regexp->counters[i].min,
  5228. regexp->counters[i].max);
  5229. }
  5230. }
  5231. /**
  5232. * xmlRegexpCompile:
  5233. * @regexp: a regular expression string
  5234. *
  5235. * Parses a regular expression conforming to XML Schemas Part 2 Datatype
  5236. * Appendix F and builds an automata suitable for testing strings against
  5237. * that regular expression
  5238. *
  5239. * Returns the compiled expression or NULL in case of error
  5240. */
  5241. xmlRegexpPtr
  5242. xmlRegexpCompile(const xmlChar *regexp) {
  5243. xmlRegexpPtr ret;
  5244. xmlRegParserCtxtPtr ctxt;
  5245. ctxt = xmlRegNewParserCtxt(regexp);
  5246. if (ctxt == NULL)
  5247. return(NULL);
  5248. /* initialize the parser */
  5249. ctxt->end = NULL;
  5250. ctxt->start = ctxt->state = xmlRegNewState(ctxt);
  5251. xmlRegStatePush(ctxt, ctxt->start);
  5252. /* parse the expression building an automata */
  5253. xmlFAParseRegExp(ctxt, 1);
  5254. if (CUR != 0) {
  5255. ERROR("xmlFAParseRegExp: extra characters");
  5256. }
  5257. if (ctxt->error != 0) {
  5258. xmlRegFreeParserCtxt(ctxt);
  5259. return(NULL);
  5260. }
  5261. ctxt->end = ctxt->state;
  5262. ctxt->start->type = XML_REGEXP_START_STATE;
  5263. ctxt->end->type = XML_REGEXP_FINAL_STATE;
  5264. /* remove the Epsilon except for counted transitions */
  5265. xmlFAEliminateEpsilonTransitions(ctxt);
  5266. if (ctxt->error != 0) {
  5267. xmlRegFreeParserCtxt(ctxt);
  5268. return(NULL);
  5269. }
  5270. ret = xmlRegEpxFromParse(ctxt);
  5271. xmlRegFreeParserCtxt(ctxt);
  5272. return(ret);
  5273. }
  5274. /**
  5275. * xmlRegexpExec:
  5276. * @comp: the compiled regular expression
  5277. * @content: the value to check against the regular expression
  5278. *
  5279. * Check if the regular expression generates the value
  5280. *
  5281. * Returns 1 if it matches, 0 if not and a negative value in case of error
  5282. */
  5283. int
  5284. xmlRegexpExec(xmlRegexpPtr comp, const xmlChar *content) {
  5285. if ((comp == NULL) || (content == NULL))
  5286. return(-1);
  5287. return(xmlFARegExec(comp, content));
  5288. }
  5289. /**
  5290. * xmlRegexpIsDeterminist:
  5291. * @comp: the compiled regular expression
  5292. *
  5293. * Check if the regular expression is determinist
  5294. *
  5295. * Returns 1 if it yes, 0 if not and a negative value in case of error
  5296. */
  5297. int
  5298. xmlRegexpIsDeterminist(xmlRegexpPtr comp) {
  5299. xmlAutomataPtr am;
  5300. int ret;
  5301. if (comp == NULL)
  5302. return(-1);
  5303. if (comp->determinist != -1)
  5304. return(comp->determinist);
  5305. am = xmlNewAutomata();
  5306. if (am == NULL)
  5307. return(-1);
  5308. if (am->states != NULL) {
  5309. int i;
  5310. for (i = 0;i < am->nbStates;i++)
  5311. xmlRegFreeState(am->states[i]);
  5312. xmlFree(am->states);
  5313. }
  5314. am->nbAtoms = comp->nbAtoms;
  5315. am->atoms = comp->atoms;
  5316. am->nbStates = comp->nbStates;
  5317. am->states = comp->states;
  5318. am->determinist = -1;
  5319. am->flags = comp->flags;
  5320. ret = xmlFAComputesDeterminism(am);
  5321. am->atoms = NULL;
  5322. am->states = NULL;
  5323. xmlFreeAutomata(am);
  5324. comp->determinist = ret;
  5325. return(ret);
  5326. }
  5327. /**
  5328. * xmlRegFreeRegexp:
  5329. * @regexp: the regexp
  5330. *
  5331. * Free a regexp
  5332. */
  5333. void
  5334. xmlRegFreeRegexp(xmlRegexpPtr regexp) {
  5335. int i;
  5336. if (regexp == NULL)
  5337. return;
  5338. if (regexp->string != NULL)
  5339. xmlFree(regexp->string);
  5340. if (regexp->states != NULL) {
  5341. for (i = 0;i < regexp->nbStates;i++)
  5342. xmlRegFreeState(regexp->states[i]);
  5343. xmlFree(regexp->states);
  5344. }
  5345. if (regexp->atoms != NULL) {
  5346. for (i = 0;i < regexp->nbAtoms;i++)
  5347. xmlRegFreeAtom(regexp->atoms[i]);
  5348. xmlFree(regexp->atoms);
  5349. }
  5350. if (regexp->counters != NULL)
  5351. xmlFree(regexp->counters);
  5352. if (regexp->compact != NULL)
  5353. xmlFree(regexp->compact);
  5354. if (regexp->transdata != NULL)
  5355. xmlFree(regexp->transdata);
  5356. if (regexp->stringMap != NULL) {
  5357. for (i = 0; i < regexp->nbstrings;i++)
  5358. xmlFree(regexp->stringMap[i]);
  5359. xmlFree(regexp->stringMap);
  5360. }
  5361. xmlFree(regexp);
  5362. }
  5363. #ifdef LIBXML_AUTOMATA_ENABLED
  5364. /************************************************************************
  5365. * *
  5366. * The Automata interface *
  5367. * *
  5368. ************************************************************************/
  5369. /**
  5370. * xmlNewAutomata:
  5371. *
  5372. * Create a new automata
  5373. *
  5374. * Returns the new object or NULL in case of failure
  5375. */
  5376. xmlAutomataPtr
  5377. xmlNewAutomata(void) {
  5378. xmlAutomataPtr ctxt;
  5379. ctxt = xmlRegNewParserCtxt(NULL);
  5380. if (ctxt == NULL)
  5381. return(NULL);
  5382. /* initialize the parser */
  5383. ctxt->end = NULL;
  5384. ctxt->start = ctxt->state = xmlRegNewState(ctxt);
  5385. if (ctxt->start == NULL) {
  5386. xmlFreeAutomata(ctxt);
  5387. return(NULL);
  5388. }
  5389. ctxt->start->type = XML_REGEXP_START_STATE;
  5390. if (xmlRegStatePush(ctxt, ctxt->start) < 0) {
  5391. xmlRegFreeState(ctxt->start);
  5392. xmlFreeAutomata(ctxt);
  5393. return(NULL);
  5394. }
  5395. ctxt->flags = 0;
  5396. return(ctxt);
  5397. }
  5398. /**
  5399. * xmlFreeAutomata:
  5400. * @am: an automata
  5401. *
  5402. * Free an automata
  5403. */
  5404. void
  5405. xmlFreeAutomata(xmlAutomataPtr am) {
  5406. if (am == NULL)
  5407. return;
  5408. xmlRegFreeParserCtxt(am);
  5409. }
  5410. /**
  5411. * xmlAutomataSetFlags:
  5412. * @am: an automata
  5413. * @flags: a set of internal flags
  5414. *
  5415. * Set some flags on the automata
  5416. */
  5417. void
  5418. xmlAutomataSetFlags(xmlAutomataPtr am, int flags) {
  5419. if (am == NULL)
  5420. return;
  5421. am->flags |= flags;
  5422. }
  5423. /**
  5424. * xmlAutomataGetInitState:
  5425. * @am: an automata
  5426. *
  5427. * Initial state lookup
  5428. *
  5429. * Returns the initial state of the automata
  5430. */
  5431. xmlAutomataStatePtr
  5432. xmlAutomataGetInitState(xmlAutomataPtr am) {
  5433. if (am == NULL)
  5434. return(NULL);
  5435. return(am->start);
  5436. }
  5437. /**
  5438. * xmlAutomataSetFinalState:
  5439. * @am: an automata
  5440. * @state: a state in this automata
  5441. *
  5442. * Makes that state a final state
  5443. *
  5444. * Returns 0 or -1 in case of error
  5445. */
  5446. int
  5447. xmlAutomataSetFinalState(xmlAutomataPtr am, xmlAutomataStatePtr state) {
  5448. if ((am == NULL) || (state == NULL))
  5449. return(-1);
  5450. state->type = XML_REGEXP_FINAL_STATE;
  5451. return(0);
  5452. }
  5453. /**
  5454. * xmlAutomataNewTransition:
  5455. * @am: an automata
  5456. * @from: the starting point of the transition
  5457. * @to: the target point of the transition or NULL
  5458. * @token: the input string associated to that transition
  5459. * @data: data passed to the callback function if the transition is activated
  5460. *
  5461. * If @to is NULL, this creates first a new target state in the automata
  5462. * and then adds a transition from the @from state to the target state
  5463. * activated by the value of @token
  5464. *
  5465. * Returns the target state or NULL in case of error
  5466. */
  5467. xmlAutomataStatePtr
  5468. xmlAutomataNewTransition(xmlAutomataPtr am, xmlAutomataStatePtr from,
  5469. xmlAutomataStatePtr to, const xmlChar *token,
  5470. void *data) {
  5471. xmlRegAtomPtr atom;
  5472. if ((am == NULL) || (from == NULL) || (token == NULL))
  5473. return(NULL);
  5474. atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
  5475. if (atom == NULL)
  5476. return(NULL);
  5477. atom->data = data;
  5478. atom->valuep = xmlStrdup(token);
  5479. if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
  5480. xmlRegFreeAtom(atom);
  5481. return(NULL);
  5482. }
  5483. if (to == NULL)
  5484. return(am->state);
  5485. return(to);
  5486. }
  5487. /**
  5488. * xmlAutomataNewTransition2:
  5489. * @am: an automata
  5490. * @from: the starting point of the transition
  5491. * @to: the target point of the transition or NULL
  5492. * @token: the first input string associated to that transition
  5493. * @token2: the second input string associated to that transition
  5494. * @data: data passed to the callback function if the transition is activated
  5495. *
  5496. * If @to is NULL, this creates first a new target state in the automata
  5497. * and then adds a transition from the @from state to the target state
  5498. * activated by the value of @token
  5499. *
  5500. * Returns the target state or NULL in case of error
  5501. */
  5502. xmlAutomataStatePtr
  5503. xmlAutomataNewTransition2(xmlAutomataPtr am, xmlAutomataStatePtr from,
  5504. xmlAutomataStatePtr to, const xmlChar *token,
  5505. const xmlChar *token2, void *data) {
  5506. xmlRegAtomPtr atom;
  5507. if ((am == NULL) || (from == NULL) || (token == NULL))
  5508. return(NULL);
  5509. atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
  5510. if (atom == NULL)
  5511. return(NULL);
  5512. atom->data = data;
  5513. if ((token2 == NULL) || (*token2 == 0)) {
  5514. atom->valuep = xmlStrdup(token);
  5515. } else {
  5516. int lenn, lenp;
  5517. xmlChar *str;
  5518. lenn = strlen((char *) token2);
  5519. lenp = strlen((char *) token);
  5520. str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
  5521. if (str == NULL) {
  5522. xmlRegFreeAtom(atom);
  5523. return(NULL);
  5524. }
  5525. memcpy(&str[0], token, lenp);
  5526. str[lenp] = '|';
  5527. memcpy(&str[lenp + 1], token2, lenn);
  5528. str[lenn + lenp + 1] = 0;
  5529. atom->valuep = str;
  5530. }
  5531. if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
  5532. xmlRegFreeAtom(atom);
  5533. return(NULL);
  5534. }
  5535. if (to == NULL)
  5536. return(am->state);
  5537. return(to);
  5538. }
  5539. /**
  5540. * xmlAutomataNewNegTrans:
  5541. * @am: an automata
  5542. * @from: the starting point of the transition
  5543. * @to: the target point of the transition or NULL
  5544. * @token: the first input string associated to that transition
  5545. * @token2: the second input string associated to that transition
  5546. * @data: data passed to the callback function if the transition is activated
  5547. *
  5548. * If @to is NULL, this creates first a new target state in the automata
  5549. * and then adds a transition from the @from state to the target state
  5550. * activated by any value except (@token,@token2)
  5551. * Note that if @token2 is not NULL, then (X, NULL) won't match to follow
  5552. # the semantic of XSD ##other
  5553. *
  5554. * Returns the target state or NULL in case of error
  5555. */
  5556. xmlAutomataStatePtr
  5557. xmlAutomataNewNegTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
  5558. xmlAutomataStatePtr to, const xmlChar *token,
  5559. const xmlChar *token2, void *data) {
  5560. xmlRegAtomPtr atom;
  5561. xmlChar err_msg[200];
  5562. if ((am == NULL) || (from == NULL) || (token == NULL))
  5563. return(NULL);
  5564. atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
  5565. if (atom == NULL)
  5566. return(NULL);
  5567. atom->data = data;
  5568. atom->neg = 1;
  5569. if ((token2 == NULL) || (*token2 == 0)) {
  5570. atom->valuep = xmlStrdup(token);
  5571. } else {
  5572. int lenn, lenp;
  5573. xmlChar *str;
  5574. lenn = strlen((char *) token2);
  5575. lenp = strlen((char *) token);
  5576. str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
  5577. if (str == NULL) {
  5578. xmlRegFreeAtom(atom);
  5579. return(NULL);
  5580. }
  5581. memcpy(&str[0], token, lenp);
  5582. str[lenp] = '|';
  5583. memcpy(&str[lenp + 1], token2, lenn);
  5584. str[lenn + lenp + 1] = 0;
  5585. atom->valuep = str;
  5586. }
  5587. snprintf((char *) err_msg, 199, "not %s", (const char *) atom->valuep);
  5588. err_msg[199] = 0;
  5589. atom->valuep2 = xmlStrdup(err_msg);
  5590. if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
  5591. xmlRegFreeAtom(atom);
  5592. return(NULL);
  5593. }
  5594. am->negs++;
  5595. if (to == NULL)
  5596. return(am->state);
  5597. return(to);
  5598. }
  5599. /**
  5600. * xmlAutomataNewCountTrans2:
  5601. * @am: an automata
  5602. * @from: the starting point of the transition
  5603. * @to: the target point of the transition or NULL
  5604. * @token: the input string associated to that transition
  5605. * @token2: the second input string associated to that transition
  5606. * @min: the minimum successive occurrences of token
  5607. * @max: the maximum successive occurrences of token
  5608. * @data: data associated to the transition
  5609. *
  5610. * If @to is NULL, this creates first a new target state in the automata
  5611. * and then adds a transition from the @from state to the target state
  5612. * activated by a succession of input of value @token and @token2 and
  5613. * whose number is between @min and @max
  5614. *
  5615. * Returns the target state or NULL in case of error
  5616. */
  5617. xmlAutomataStatePtr
  5618. xmlAutomataNewCountTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from,
  5619. xmlAutomataStatePtr to, const xmlChar *token,
  5620. const xmlChar *token2,
  5621. int min, int max, void *data) {
  5622. xmlRegAtomPtr atom;
  5623. int counter;
  5624. if ((am == NULL) || (from == NULL) || (token == NULL))
  5625. return(NULL);
  5626. if (min < 0)
  5627. return(NULL);
  5628. if ((max < min) || (max < 1))
  5629. return(NULL);
  5630. atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
  5631. if (atom == NULL)
  5632. return(NULL);
  5633. if ((token2 == NULL) || (*token2 == 0)) {
  5634. atom->valuep = xmlStrdup(token);
  5635. } else {
  5636. int lenn, lenp;
  5637. xmlChar *str;
  5638. lenn = strlen((char *) token2);
  5639. lenp = strlen((char *) token);
  5640. str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
  5641. if (str == NULL) {
  5642. xmlRegFreeAtom(atom);
  5643. return(NULL);
  5644. }
  5645. memcpy(&str[0], token, lenp);
  5646. str[lenp] = '|';
  5647. memcpy(&str[lenp + 1], token2, lenn);
  5648. str[lenn + lenp + 1] = 0;
  5649. atom->valuep = str;
  5650. }
  5651. atom->data = data;
  5652. if (min == 0)
  5653. atom->min = 1;
  5654. else
  5655. atom->min = min;
  5656. atom->max = max;
  5657. /*
  5658. * associate a counter to the transition.
  5659. */
  5660. counter = xmlRegGetCounter(am);
  5661. am->counters[counter].min = min;
  5662. am->counters[counter].max = max;
  5663. /* xmlFAGenerateTransitions(am, from, to, atom); */
  5664. if (to == NULL) {
  5665. to = xmlRegNewState(am);
  5666. xmlRegStatePush(am, to);
  5667. }
  5668. xmlRegStateAddTrans(am, from, atom, to, counter, -1);
  5669. xmlRegAtomPush(am, atom);
  5670. am->state = to;
  5671. if (to == NULL)
  5672. to = am->state;
  5673. if (to == NULL)
  5674. return(NULL);
  5675. if (min == 0)
  5676. xmlFAGenerateEpsilonTransition(am, from, to);
  5677. return(to);
  5678. }
  5679. /**
  5680. * xmlAutomataNewCountTrans:
  5681. * @am: an automata
  5682. * @from: the starting point of the transition
  5683. * @to: the target point of the transition or NULL
  5684. * @token: the input string associated to that transition
  5685. * @min: the minimum successive occurrences of token
  5686. * @max: the maximum successive occurrences of token
  5687. * @data: data associated to the transition
  5688. *
  5689. * If @to is NULL, this creates first a new target state in the automata
  5690. * and then adds a transition from the @from state to the target state
  5691. * activated by a succession of input of value @token and whose number
  5692. * is between @min and @max
  5693. *
  5694. * Returns the target state or NULL in case of error
  5695. */
  5696. xmlAutomataStatePtr
  5697. xmlAutomataNewCountTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
  5698. xmlAutomataStatePtr to, const xmlChar *token,
  5699. int min, int max, void *data) {
  5700. xmlRegAtomPtr atom;
  5701. int counter;
  5702. if ((am == NULL) || (from == NULL) || (token == NULL))
  5703. return(NULL);
  5704. if (min < 0)
  5705. return(NULL);
  5706. if ((max < min) || (max < 1))
  5707. return(NULL);
  5708. atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
  5709. if (atom == NULL)
  5710. return(NULL);
  5711. atom->valuep = xmlStrdup(token);
  5712. atom->data = data;
  5713. if (min == 0)
  5714. atom->min = 1;
  5715. else
  5716. atom->min = min;
  5717. atom->max = max;
  5718. /*
  5719. * associate a counter to the transition.
  5720. */
  5721. counter = xmlRegGetCounter(am);
  5722. am->counters[counter].min = min;
  5723. am->counters[counter].max = max;
  5724. /* xmlFAGenerateTransitions(am, from, to, atom); */
  5725. if (to == NULL) {
  5726. to = xmlRegNewState(am);
  5727. xmlRegStatePush(am, to);
  5728. }
  5729. xmlRegStateAddTrans(am, from, atom, to, counter, -1);
  5730. xmlRegAtomPush(am, atom);
  5731. am->state = to;
  5732. if (to == NULL)
  5733. to = am->state;
  5734. if (to == NULL)
  5735. return(NULL);
  5736. if (min == 0)
  5737. xmlFAGenerateEpsilonTransition(am, from, to);
  5738. return(to);
  5739. }
  5740. /**
  5741. * xmlAutomataNewOnceTrans2:
  5742. * @am: an automata
  5743. * @from: the starting point of the transition
  5744. * @to: the target point of the transition or NULL
  5745. * @token: the input string associated to that transition
  5746. * @token2: the second input string associated to that transition
  5747. * @min: the minimum successive occurrences of token
  5748. * @max: the maximum successive occurrences of token
  5749. * @data: data associated to the transition
  5750. *
  5751. * If @to is NULL, this creates first a new target state in the automata
  5752. * and then adds a transition from the @from state to the target state
  5753. * activated by a succession of input of value @token and @token2 and whose
  5754. * number is between @min and @max, moreover that transition can only be
  5755. * crossed once.
  5756. *
  5757. * Returns the target state or NULL in case of error
  5758. */
  5759. xmlAutomataStatePtr
  5760. xmlAutomataNewOnceTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from,
  5761. xmlAutomataStatePtr to, const xmlChar *token,
  5762. const xmlChar *token2,
  5763. int min, int max, void *data) {
  5764. xmlRegAtomPtr atom;
  5765. int counter;
  5766. if ((am == NULL) || (from == NULL) || (token == NULL))
  5767. return(NULL);
  5768. if (min < 1)
  5769. return(NULL);
  5770. if (max < min)
  5771. return(NULL);
  5772. atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
  5773. if (atom == NULL)
  5774. return(NULL);
  5775. if ((token2 == NULL) || (*token2 == 0)) {
  5776. atom->valuep = xmlStrdup(token);
  5777. } else {
  5778. int lenn, lenp;
  5779. xmlChar *str;
  5780. lenn = strlen((char *) token2);
  5781. lenp = strlen((char *) token);
  5782. str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
  5783. if (str == NULL) {
  5784. xmlRegFreeAtom(atom);
  5785. return(NULL);
  5786. }
  5787. memcpy(&str[0], token, lenp);
  5788. str[lenp] = '|';
  5789. memcpy(&str[lenp + 1], token2, lenn);
  5790. str[lenn + lenp + 1] = 0;
  5791. atom->valuep = str;
  5792. }
  5793. atom->data = data;
  5794. atom->quant = XML_REGEXP_QUANT_ONCEONLY;
  5795. atom->min = min;
  5796. atom->max = max;
  5797. /*
  5798. * associate a counter to the transition.
  5799. */
  5800. counter = xmlRegGetCounter(am);
  5801. am->counters[counter].min = 1;
  5802. am->counters[counter].max = 1;
  5803. /* xmlFAGenerateTransitions(am, from, to, atom); */
  5804. if (to == NULL) {
  5805. to = xmlRegNewState(am);
  5806. xmlRegStatePush(am, to);
  5807. }
  5808. xmlRegStateAddTrans(am, from, atom, to, counter, -1);
  5809. xmlRegAtomPush(am, atom);
  5810. am->state = to;
  5811. return(to);
  5812. }
  5813. /**
  5814. * xmlAutomataNewOnceTrans:
  5815. * @am: an automata
  5816. * @from: the starting point of the transition
  5817. * @to: the target point of the transition or NULL
  5818. * @token: the input string associated to that transition
  5819. * @min: the minimum successive occurrences of token
  5820. * @max: the maximum successive occurrences of token
  5821. * @data: data associated to the transition
  5822. *
  5823. * If @to is NULL, this creates first a new target state in the automata
  5824. * and then adds a transition from the @from state to the target state
  5825. * activated by a succession of input of value @token and whose number
  5826. * is between @min and @max, moreover that transition can only be crossed
  5827. * once.
  5828. *
  5829. * Returns the target state or NULL in case of error
  5830. */
  5831. xmlAutomataStatePtr
  5832. xmlAutomataNewOnceTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
  5833. xmlAutomataStatePtr to, const xmlChar *token,
  5834. int min, int max, void *data) {
  5835. xmlRegAtomPtr atom;
  5836. int counter;
  5837. if ((am == NULL) || (from == NULL) || (token == NULL))
  5838. return(NULL);
  5839. if (min < 1)
  5840. return(NULL);
  5841. if (max < min)
  5842. return(NULL);
  5843. atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
  5844. if (atom == NULL)
  5845. return(NULL);
  5846. atom->valuep = xmlStrdup(token);
  5847. atom->data = data;
  5848. atom->quant = XML_REGEXP_QUANT_ONCEONLY;
  5849. atom->min = min;
  5850. atom->max = max;
  5851. /*
  5852. * associate a counter to the transition.
  5853. */
  5854. counter = xmlRegGetCounter(am);
  5855. am->counters[counter].min = 1;
  5856. am->counters[counter].max = 1;
  5857. /* xmlFAGenerateTransitions(am, from, to, atom); */
  5858. if (to == NULL) {
  5859. to = xmlRegNewState(am);
  5860. xmlRegStatePush(am, to);
  5861. }
  5862. xmlRegStateAddTrans(am, from, atom, to, counter, -1);
  5863. xmlRegAtomPush(am, atom);
  5864. am->state = to;
  5865. return(to);
  5866. }
  5867. /**
  5868. * xmlAutomataNewState:
  5869. * @am: an automata
  5870. *
  5871. * Create a new disconnected state in the automata
  5872. *
  5873. * Returns the new state or NULL in case of error
  5874. */
  5875. xmlAutomataStatePtr
  5876. xmlAutomataNewState(xmlAutomataPtr am) {
  5877. xmlAutomataStatePtr to;
  5878. if (am == NULL)
  5879. return(NULL);
  5880. to = xmlRegNewState(am);
  5881. xmlRegStatePush(am, to);
  5882. return(to);
  5883. }
  5884. /**
  5885. * xmlAutomataNewEpsilon:
  5886. * @am: an automata
  5887. * @from: the starting point of the transition
  5888. * @to: the target point of the transition or NULL
  5889. *
  5890. * If @to is NULL, this creates first a new target state in the automata
  5891. * and then adds an epsilon transition from the @from state to the
  5892. * target state
  5893. *
  5894. * Returns the target state or NULL in case of error
  5895. */
  5896. xmlAutomataStatePtr
  5897. xmlAutomataNewEpsilon(xmlAutomataPtr am, xmlAutomataStatePtr from,
  5898. xmlAutomataStatePtr to) {
  5899. if ((am == NULL) || (from == NULL))
  5900. return(NULL);
  5901. xmlFAGenerateEpsilonTransition(am, from, to);
  5902. if (to == NULL)
  5903. return(am->state);
  5904. return(to);
  5905. }
  5906. /**
  5907. * xmlAutomataNewAllTrans:
  5908. * @am: an automata
  5909. * @from: the starting point of the transition
  5910. * @to: the target point of the transition or NULL
  5911. * @lax: allow to transition if not all all transitions have been activated
  5912. *
  5913. * If @to is NULL, this creates first a new target state in the automata
  5914. * and then adds a an ALL transition from the @from state to the
  5915. * target state. That transition is an epsilon transition allowed only when
  5916. * all transitions from the @from node have been activated.
  5917. *
  5918. * Returns the target state or NULL in case of error
  5919. */
  5920. xmlAutomataStatePtr
  5921. xmlAutomataNewAllTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
  5922. xmlAutomataStatePtr to, int lax) {
  5923. if ((am == NULL) || (from == NULL))
  5924. return(NULL);
  5925. xmlFAGenerateAllTransition(am, from, to, lax);
  5926. if (to == NULL)
  5927. return(am->state);
  5928. return(to);
  5929. }
  5930. /**
  5931. * xmlAutomataNewCounter:
  5932. * @am: an automata
  5933. * @min: the minimal value on the counter
  5934. * @max: the maximal value on the counter
  5935. *
  5936. * Create a new counter
  5937. *
  5938. * Returns the counter number or -1 in case of error
  5939. */
  5940. int
  5941. xmlAutomataNewCounter(xmlAutomataPtr am, int min, int max) {
  5942. int ret;
  5943. if (am == NULL)
  5944. return(-1);
  5945. ret = xmlRegGetCounter(am);
  5946. if (ret < 0)
  5947. return(-1);
  5948. am->counters[ret].min = min;
  5949. am->counters[ret].max = max;
  5950. return(ret);
  5951. }
  5952. /**
  5953. * xmlAutomataNewCountedTrans:
  5954. * @am: an automata
  5955. * @from: the starting point of the transition
  5956. * @to: the target point of the transition or NULL
  5957. * @counter: the counter associated to that transition
  5958. *
  5959. * If @to is NULL, this creates first a new target state in the automata
  5960. * and then adds an epsilon transition from the @from state to the target state
  5961. * which will increment the counter provided
  5962. *
  5963. * Returns the target state or NULL in case of error
  5964. */
  5965. xmlAutomataStatePtr
  5966. xmlAutomataNewCountedTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
  5967. xmlAutomataStatePtr to, int counter) {
  5968. if ((am == NULL) || (from == NULL) || (counter < 0))
  5969. return(NULL);
  5970. xmlFAGenerateCountedEpsilonTransition(am, from, to, counter);
  5971. if (to == NULL)
  5972. return(am->state);
  5973. return(to);
  5974. }
  5975. /**
  5976. * xmlAutomataNewCounterTrans:
  5977. * @am: an automata
  5978. * @from: the starting point of the transition
  5979. * @to: the target point of the transition or NULL
  5980. * @counter: the counter associated to that transition
  5981. *
  5982. * If @to is NULL, this creates first a new target state in the automata
  5983. * and then adds an epsilon transition from the @from state to the target state
  5984. * which will be allowed only if the counter is within the right range.
  5985. *
  5986. * Returns the target state or NULL in case of error
  5987. */
  5988. xmlAutomataStatePtr
  5989. xmlAutomataNewCounterTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
  5990. xmlAutomataStatePtr to, int counter) {
  5991. if ((am == NULL) || (from == NULL) || (counter < 0))
  5992. return(NULL);
  5993. xmlFAGenerateCountedTransition(am, from, to, counter);
  5994. if (to == NULL)
  5995. return(am->state);
  5996. return(to);
  5997. }
  5998. /**
  5999. * xmlAutomataCompile:
  6000. * @am: an automata
  6001. *
  6002. * Compile the automata into a Reg Exp ready for being executed.
  6003. * The automata should be free after this point.
  6004. *
  6005. * Returns the compiled regexp or NULL in case of error
  6006. */
  6007. xmlRegexpPtr
  6008. xmlAutomataCompile(xmlAutomataPtr am) {
  6009. xmlRegexpPtr ret;
  6010. if ((am == NULL) || (am->error != 0)) return(NULL);
  6011. xmlFAEliminateEpsilonTransitions(am);
  6012. /* xmlFAComputesDeterminism(am); */
  6013. ret = xmlRegEpxFromParse(am);
  6014. return(ret);
  6015. }
  6016. /**
  6017. * xmlAutomataIsDeterminist:
  6018. * @am: an automata
  6019. *
  6020. * Checks if an automata is determinist.
  6021. *
  6022. * Returns 1 if true, 0 if not, and -1 in case of error
  6023. */
  6024. int
  6025. xmlAutomataIsDeterminist(xmlAutomataPtr am) {
  6026. int ret;
  6027. if (am == NULL)
  6028. return(-1);
  6029. ret = xmlFAComputesDeterminism(am);
  6030. return(ret);
  6031. }
  6032. #endif /* LIBXML_AUTOMATA_ENABLED */
  6033. #ifdef LIBXML_EXPR_ENABLED
  6034. /************************************************************************
  6035. * *
  6036. * Formal Expression handling code *
  6037. * *
  6038. ************************************************************************/
  6039. /************************************************************************
  6040. * *
  6041. * Expression handling context *
  6042. * *
  6043. ************************************************************************/
  6044. struct _xmlExpCtxt {
  6045. xmlDictPtr dict;
  6046. xmlExpNodePtr *table;
  6047. int size;
  6048. int nbElems;
  6049. int nb_nodes;
  6050. int maxNodes;
  6051. const char *expr;
  6052. const char *cur;
  6053. int nb_cons;
  6054. int tabSize;
  6055. };
  6056. /**
  6057. * xmlExpNewCtxt:
  6058. * @maxNodes: the maximum number of nodes
  6059. * @dict: optional dictionary to use internally
  6060. *
  6061. * Creates a new context for manipulating expressions
  6062. *
  6063. * Returns the context or NULL in case of error
  6064. */
  6065. xmlExpCtxtPtr
  6066. xmlExpNewCtxt(int maxNodes, xmlDictPtr dict) {
  6067. xmlExpCtxtPtr ret;
  6068. int size = 256;
  6069. if (maxNodes <= 4096)
  6070. maxNodes = 4096;
  6071. ret = (xmlExpCtxtPtr) xmlMalloc(sizeof(xmlExpCtxt));
  6072. if (ret == NULL)
  6073. return(NULL);
  6074. memset(ret, 0, sizeof(xmlExpCtxt));
  6075. ret->size = size;
  6076. ret->nbElems = 0;
  6077. ret->maxNodes = maxNodes;
  6078. ret->table = xmlMalloc(size * sizeof(xmlExpNodePtr));
  6079. if (ret->table == NULL) {
  6080. xmlFree(ret);
  6081. return(NULL);
  6082. }
  6083. memset(ret->table, 0, size * sizeof(xmlExpNodePtr));
  6084. if (dict == NULL) {
  6085. ret->dict = xmlDictCreate();
  6086. if (ret->dict == NULL) {
  6087. xmlFree(ret->table);
  6088. xmlFree(ret);
  6089. return(NULL);
  6090. }
  6091. } else {
  6092. ret->dict = dict;
  6093. xmlDictReference(ret->dict);
  6094. }
  6095. return(ret);
  6096. }
  6097. /**
  6098. * xmlExpFreeCtxt:
  6099. * @ctxt: an expression context
  6100. *
  6101. * Free an expression context
  6102. */
  6103. void
  6104. xmlExpFreeCtxt(xmlExpCtxtPtr ctxt) {
  6105. if (ctxt == NULL)
  6106. return;
  6107. xmlDictFree(ctxt->dict);
  6108. if (ctxt->table != NULL)
  6109. xmlFree(ctxt->table);
  6110. xmlFree(ctxt);
  6111. }
  6112. /************************************************************************
  6113. * *
  6114. * Structure associated to an expression node *
  6115. * *
  6116. ************************************************************************/
  6117. #define MAX_NODES 10000
  6118. /* #define DEBUG_DERIV */
  6119. /*
  6120. * TODO:
  6121. * - Wildcards
  6122. * - public API for creation
  6123. *
  6124. * Started
  6125. * - regression testing
  6126. *
  6127. * Done
  6128. * - split into module and test tool
  6129. * - memleaks
  6130. */
  6131. typedef enum {
  6132. XML_EXP_NILABLE = (1 << 0)
  6133. } xmlExpNodeInfo;
  6134. #define IS_NILLABLE(node) ((node)->info & XML_EXP_NILABLE)
  6135. struct _xmlExpNode {
  6136. unsigned char type;/* xmlExpNodeType */
  6137. unsigned char info;/* OR of xmlExpNodeInfo */
  6138. unsigned short key; /* the hash key */
  6139. unsigned int ref; /* The number of references */
  6140. int c_max; /* the maximum length it can consume */
  6141. xmlExpNodePtr exp_left;
  6142. xmlExpNodePtr next;/* the next node in the hash table or free list */
  6143. union {
  6144. struct {
  6145. int f_min;
  6146. int f_max;
  6147. } count;
  6148. struct {
  6149. xmlExpNodePtr f_right;
  6150. } children;
  6151. const xmlChar *f_str;
  6152. } field;
  6153. };
  6154. #define exp_min field.count.f_min
  6155. #define exp_max field.count.f_max
  6156. /* #define exp_left field.children.f_left */
  6157. #define exp_right field.children.f_right
  6158. #define exp_str field.f_str
  6159. static xmlExpNodePtr xmlExpNewNode(xmlExpCtxtPtr ctxt, xmlExpNodeType type);
  6160. static xmlExpNode forbiddenExpNode = {
  6161. XML_EXP_FORBID, 0, 0, 0, 0, NULL, NULL, {{ 0, 0}}
  6162. };
  6163. xmlExpNodePtr forbiddenExp = &forbiddenExpNode;
  6164. static xmlExpNode emptyExpNode = {
  6165. XML_EXP_EMPTY, 1, 0, 0, 0, NULL, NULL, {{ 0, 0}}
  6166. };
  6167. xmlExpNodePtr emptyExp = &emptyExpNode;
  6168. /************************************************************************
  6169. * *
  6170. * The custom hash table for unicity and canonicalization *
  6171. * of sub-expressions pointers *
  6172. * *
  6173. ************************************************************************/
  6174. /*
  6175. * xmlExpHashNameComputeKey:
  6176. * Calculate the hash key for a token
  6177. */
  6178. static unsigned short
  6179. xmlExpHashNameComputeKey(const xmlChar *name) {
  6180. unsigned short value = 0L;
  6181. char ch;
  6182. if (name != NULL) {
  6183. value += 30 * (*name);
  6184. while ((ch = *name++) != 0) {
  6185. value = value ^ ((value << 5) + (value >> 3) + (unsigned long)ch);
  6186. }
  6187. }
  6188. return (value);
  6189. }
  6190. /*
  6191. * xmlExpHashComputeKey:
  6192. * Calculate the hash key for a compound expression
  6193. */
  6194. static unsigned short
  6195. xmlExpHashComputeKey(xmlExpNodeType type, xmlExpNodePtr left,
  6196. xmlExpNodePtr right) {
  6197. unsigned long value;
  6198. unsigned short ret;
  6199. switch (type) {
  6200. case XML_EXP_SEQ:
  6201. value = left->key;
  6202. value += right->key;
  6203. value *= 3;
  6204. ret = (unsigned short) value;
  6205. break;
  6206. case XML_EXP_OR:
  6207. value = left->key;
  6208. value += right->key;
  6209. value *= 7;
  6210. ret = (unsigned short) value;
  6211. break;
  6212. case XML_EXP_COUNT:
  6213. value = left->key;
  6214. value += right->key;
  6215. ret = (unsigned short) value;
  6216. break;
  6217. default:
  6218. ret = 0;
  6219. }
  6220. return(ret);
  6221. }
  6222. static xmlExpNodePtr
  6223. xmlExpNewNode(xmlExpCtxtPtr ctxt, xmlExpNodeType type) {
  6224. xmlExpNodePtr ret;
  6225. if (ctxt->nb_nodes >= MAX_NODES)
  6226. return(NULL);
  6227. ret = (xmlExpNodePtr) xmlMalloc(sizeof(xmlExpNode));
  6228. if (ret == NULL)
  6229. return(NULL);
  6230. memset(ret, 0, sizeof(xmlExpNode));
  6231. ret->type = type;
  6232. ret->next = NULL;
  6233. ctxt->nb_nodes++;
  6234. ctxt->nb_cons++;
  6235. return(ret);
  6236. }
  6237. /**
  6238. * xmlExpHashGetEntry:
  6239. * @table: the hash table
  6240. *
  6241. * Get the unique entry from the hash table. The entry is created if
  6242. * needed. @left and @right are consumed, i.e. their ref count will
  6243. * be decremented by the operation.
  6244. *
  6245. * Returns the pointer or NULL in case of error
  6246. */
  6247. static xmlExpNodePtr
  6248. xmlExpHashGetEntry(xmlExpCtxtPtr ctxt, xmlExpNodeType type,
  6249. xmlExpNodePtr left, xmlExpNodePtr right,
  6250. const xmlChar *name, int min, int max) {
  6251. unsigned short kbase, key;
  6252. xmlExpNodePtr entry;
  6253. xmlExpNodePtr insert;
  6254. if (ctxt == NULL)
  6255. return(NULL);
  6256. /*
  6257. * Check for duplicate and insertion location.
  6258. */
  6259. if (type == XML_EXP_ATOM) {
  6260. kbase = xmlExpHashNameComputeKey(name);
  6261. } else if (type == XML_EXP_COUNT) {
  6262. /* COUNT reduction rule 1 */
  6263. /* a{1} -> a */
  6264. if (min == max) {
  6265. if (min == 1) {
  6266. return(left);
  6267. }
  6268. if (min == 0) {
  6269. xmlExpFree(ctxt, left);
  6270. return(emptyExp);
  6271. }
  6272. }
  6273. if (min < 0) {
  6274. xmlExpFree(ctxt, left);
  6275. return(forbiddenExp);
  6276. }
  6277. if (max == -1)
  6278. kbase = min + 79;
  6279. else
  6280. kbase = max - min;
  6281. kbase += left->key;
  6282. } else if (type == XML_EXP_OR) {
  6283. /* Forbid reduction rules */
  6284. if (left->type == XML_EXP_FORBID) {
  6285. xmlExpFree(ctxt, left);
  6286. return(right);
  6287. }
  6288. if (right->type == XML_EXP_FORBID) {
  6289. xmlExpFree(ctxt, right);
  6290. return(left);
  6291. }
  6292. /* OR reduction rule 1 */
  6293. /* a | a reduced to a */
  6294. if (left == right) {
  6295. xmlExpFree(ctxt, right);
  6296. return(left);
  6297. }
  6298. /* OR canonicalization rule 1 */
  6299. /* linearize (a | b) | c into a | (b | c) */
  6300. if ((left->type == XML_EXP_OR) && (right->type != XML_EXP_OR)) {
  6301. xmlExpNodePtr tmp = left;
  6302. left = right;
  6303. right = tmp;
  6304. }
  6305. /* OR reduction rule 2 */
  6306. /* a | (a | b) and b | (a | b) are reduced to a | b */
  6307. if (right->type == XML_EXP_OR) {
  6308. if ((left == right->exp_left) ||
  6309. (left == right->exp_right)) {
  6310. xmlExpFree(ctxt, left);
  6311. return(right);
  6312. }
  6313. }
  6314. /* OR canonicalization rule 2 */
  6315. /* linearize (a | b) | c into a | (b | c) */
  6316. if (left->type == XML_EXP_OR) {
  6317. xmlExpNodePtr tmp;
  6318. /* OR canonicalization rule 2 */
  6319. if ((left->exp_right->type != XML_EXP_OR) &&
  6320. (left->exp_right->key < left->exp_left->key)) {
  6321. tmp = left->exp_right;
  6322. left->exp_right = left->exp_left;
  6323. left->exp_left = tmp;
  6324. }
  6325. left->exp_right->ref++;
  6326. tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left->exp_right, right,
  6327. NULL, 0, 0);
  6328. left->exp_left->ref++;
  6329. tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left->exp_left, tmp,
  6330. NULL, 0, 0);
  6331. xmlExpFree(ctxt, left);
  6332. return(tmp);
  6333. }
  6334. if (right->type == XML_EXP_OR) {
  6335. /* Ordering in the tree */
  6336. /* C | (A | B) -> A | (B | C) */
  6337. if (left->key > right->exp_right->key) {
  6338. xmlExpNodePtr tmp;
  6339. right->exp_right->ref++;
  6340. tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_right,
  6341. left, NULL, 0, 0);
  6342. right->exp_left->ref++;
  6343. tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_left,
  6344. tmp, NULL, 0, 0);
  6345. xmlExpFree(ctxt, right);
  6346. return(tmp);
  6347. }
  6348. /* Ordering in the tree */
  6349. /* B | (A | C) -> A | (B | C) */
  6350. if (left->key > right->exp_left->key) {
  6351. xmlExpNodePtr tmp;
  6352. right->exp_right->ref++;
  6353. tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left,
  6354. right->exp_right, NULL, 0, 0);
  6355. right->exp_left->ref++;
  6356. tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_left,
  6357. tmp, NULL, 0, 0);
  6358. xmlExpFree(ctxt, right);
  6359. return(tmp);
  6360. }
  6361. }
  6362. /* we know both types are != XML_EXP_OR here */
  6363. else if (left->key > right->key) {
  6364. xmlExpNodePtr tmp = left;
  6365. left = right;
  6366. right = tmp;
  6367. }
  6368. kbase = xmlExpHashComputeKey(type, left, right);
  6369. } else if (type == XML_EXP_SEQ) {
  6370. /* Forbid reduction rules */
  6371. if (left->type == XML_EXP_FORBID) {
  6372. xmlExpFree(ctxt, right);
  6373. return(left);
  6374. }
  6375. if (right->type == XML_EXP_FORBID) {
  6376. xmlExpFree(ctxt, left);
  6377. return(right);
  6378. }
  6379. /* Empty reduction rules */
  6380. if (right->type == XML_EXP_EMPTY) {
  6381. return(left);
  6382. }
  6383. if (left->type == XML_EXP_EMPTY) {
  6384. return(right);
  6385. }
  6386. kbase = xmlExpHashComputeKey(type, left, right);
  6387. } else
  6388. return(NULL);
  6389. key = kbase % ctxt->size;
  6390. if (ctxt->table[key] != NULL) {
  6391. for (insert = ctxt->table[key]; insert != NULL;
  6392. insert = insert->next) {
  6393. if ((insert->key == kbase) &&
  6394. (insert->type == type)) {
  6395. if (type == XML_EXP_ATOM) {
  6396. if (name == insert->exp_str) {
  6397. insert->ref++;
  6398. return(insert);
  6399. }
  6400. } else if (type == XML_EXP_COUNT) {
  6401. if ((insert->exp_min == min) && (insert->exp_max == max) &&
  6402. (insert->exp_left == left)) {
  6403. insert->ref++;
  6404. left->ref--;
  6405. return(insert);
  6406. }
  6407. } else if ((insert->exp_left == left) &&
  6408. (insert->exp_right == right)) {
  6409. insert->ref++;
  6410. left->ref--;
  6411. right->ref--;
  6412. return(insert);
  6413. }
  6414. }
  6415. }
  6416. }
  6417. entry = xmlExpNewNode(ctxt, type);
  6418. if (entry == NULL)
  6419. return(NULL);
  6420. entry->key = kbase;
  6421. if (type == XML_EXP_ATOM) {
  6422. entry->exp_str = name;
  6423. entry->c_max = 1;
  6424. } else if (type == XML_EXP_COUNT) {
  6425. entry->exp_min = min;
  6426. entry->exp_max = max;
  6427. entry->exp_left = left;
  6428. if ((min == 0) || (IS_NILLABLE(left)))
  6429. entry->info |= XML_EXP_NILABLE;
  6430. if (max < 0)
  6431. entry->c_max = -1;
  6432. else
  6433. entry->c_max = max * entry->exp_left->c_max;
  6434. } else {
  6435. entry->exp_left = left;
  6436. entry->exp_right = right;
  6437. if (type == XML_EXP_OR) {
  6438. if ((IS_NILLABLE(left)) || (IS_NILLABLE(right)))
  6439. entry->info |= XML_EXP_NILABLE;
  6440. if ((entry->exp_left->c_max == -1) ||
  6441. (entry->exp_right->c_max == -1))
  6442. entry->c_max = -1;
  6443. else if (entry->exp_left->c_max > entry->exp_right->c_max)
  6444. entry->c_max = entry->exp_left->c_max;
  6445. else
  6446. entry->c_max = entry->exp_right->c_max;
  6447. } else {
  6448. if ((IS_NILLABLE(left)) && (IS_NILLABLE(right)))
  6449. entry->info |= XML_EXP_NILABLE;
  6450. if ((entry->exp_left->c_max == -1) ||
  6451. (entry->exp_right->c_max == -1))
  6452. entry->c_max = -1;
  6453. else
  6454. entry->c_max = entry->exp_left->c_max + entry->exp_right->c_max;
  6455. }
  6456. }
  6457. entry->ref = 1;
  6458. if (ctxt->table[key] != NULL)
  6459. entry->next = ctxt->table[key];
  6460. ctxt->table[key] = entry;
  6461. ctxt->nbElems++;
  6462. return(entry);
  6463. }
  6464. /**
  6465. * xmlExpFree:
  6466. * @ctxt: the expression context
  6467. * @exp: the expression
  6468. *
  6469. * Dereference the expression
  6470. */
  6471. void
  6472. xmlExpFree(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp) {
  6473. if ((exp == NULL) || (exp == forbiddenExp) || (exp == emptyExp))
  6474. return;
  6475. exp->ref--;
  6476. if (exp->ref == 0) {
  6477. unsigned short key;
  6478. /* Unlink it first from the hash table */
  6479. key = exp->key % ctxt->size;
  6480. if (ctxt->table[key] == exp) {
  6481. ctxt->table[key] = exp->next;
  6482. } else {
  6483. xmlExpNodePtr tmp;
  6484. tmp = ctxt->table[key];
  6485. while (tmp != NULL) {
  6486. if (tmp->next == exp) {
  6487. tmp->next = exp->next;
  6488. break;
  6489. }
  6490. tmp = tmp->next;
  6491. }
  6492. }
  6493. if ((exp->type == XML_EXP_SEQ) || (exp->type == XML_EXP_OR)) {
  6494. xmlExpFree(ctxt, exp->exp_left);
  6495. xmlExpFree(ctxt, exp->exp_right);
  6496. } else if (exp->type == XML_EXP_COUNT) {
  6497. xmlExpFree(ctxt, exp->exp_left);
  6498. }
  6499. xmlFree(exp);
  6500. ctxt->nb_nodes--;
  6501. }
  6502. }
  6503. /**
  6504. * xmlExpRef:
  6505. * @exp: the expression
  6506. *
  6507. * Increase the reference count of the expression
  6508. */
  6509. void
  6510. xmlExpRef(xmlExpNodePtr exp) {
  6511. if (exp != NULL)
  6512. exp->ref++;
  6513. }
  6514. /**
  6515. * xmlExpNewAtom:
  6516. * @ctxt: the expression context
  6517. * @name: the atom name
  6518. * @len: the atom name length in byte (or -1);
  6519. *
  6520. * Get the atom associated to this name from that context
  6521. *
  6522. * Returns the node or NULL in case of error
  6523. */
  6524. xmlExpNodePtr
  6525. xmlExpNewAtom(xmlExpCtxtPtr ctxt, const xmlChar *name, int len) {
  6526. if ((ctxt == NULL) || (name == NULL))
  6527. return(NULL);
  6528. name = xmlDictLookup(ctxt->dict, name, len);
  6529. if (name == NULL)
  6530. return(NULL);
  6531. return(xmlExpHashGetEntry(ctxt, XML_EXP_ATOM, NULL, NULL, name, 0, 0));
  6532. }
  6533. /**
  6534. * xmlExpNewOr:
  6535. * @ctxt: the expression context
  6536. * @left: left expression
  6537. * @right: right expression
  6538. *
  6539. * Get the atom associated to the choice @left | @right
  6540. * Note that @left and @right are consumed in the operation, to keep
  6541. * an handle on them use xmlExpRef() and use xmlExpFree() to release them,
  6542. * this is true even in case of failure (unless ctxt == NULL).
  6543. *
  6544. * Returns the node or NULL in case of error
  6545. */
  6546. xmlExpNodePtr
  6547. xmlExpNewOr(xmlExpCtxtPtr ctxt, xmlExpNodePtr left, xmlExpNodePtr right) {
  6548. if (ctxt == NULL)
  6549. return(NULL);
  6550. if ((left == NULL) || (right == NULL)) {
  6551. xmlExpFree(ctxt, left);
  6552. xmlExpFree(ctxt, right);
  6553. return(NULL);
  6554. }
  6555. return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, left, right, NULL, 0, 0));
  6556. }
  6557. /**
  6558. * xmlExpNewSeq:
  6559. * @ctxt: the expression context
  6560. * @left: left expression
  6561. * @right: right expression
  6562. *
  6563. * Get the atom associated to the sequence @left , @right
  6564. * Note that @left and @right are consumed in the operation, to keep
  6565. * an handle on them use xmlExpRef() and use xmlExpFree() to release them,
  6566. * this is true even in case of failure (unless ctxt == NULL).
  6567. *
  6568. * Returns the node or NULL in case of error
  6569. */
  6570. xmlExpNodePtr
  6571. xmlExpNewSeq(xmlExpCtxtPtr ctxt, xmlExpNodePtr left, xmlExpNodePtr right) {
  6572. if (ctxt == NULL)
  6573. return(NULL);
  6574. if ((left == NULL) || (right == NULL)) {
  6575. xmlExpFree(ctxt, left);
  6576. xmlExpFree(ctxt, right);
  6577. return(NULL);
  6578. }
  6579. return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, left, right, NULL, 0, 0));
  6580. }
  6581. /**
  6582. * xmlExpNewRange:
  6583. * @ctxt: the expression context
  6584. * @subset: the expression to be repeated
  6585. * @min: the lower bound for the repetition
  6586. * @max: the upper bound for the repetition, -1 means infinite
  6587. *
  6588. * Get the atom associated to the range (@subset){@min, @max}
  6589. * Note that @subset is consumed in the operation, to keep
  6590. * an handle on it use xmlExpRef() and use xmlExpFree() to release it,
  6591. * this is true even in case of failure (unless ctxt == NULL).
  6592. *
  6593. * Returns the node or NULL in case of error
  6594. */
  6595. xmlExpNodePtr
  6596. xmlExpNewRange(xmlExpCtxtPtr ctxt, xmlExpNodePtr subset, int min, int max) {
  6597. if (ctxt == NULL)
  6598. return(NULL);
  6599. if ((subset == NULL) || (min < 0) || (max < -1) ||
  6600. ((max >= 0) && (min > max))) {
  6601. xmlExpFree(ctxt, subset);
  6602. return(NULL);
  6603. }
  6604. return(xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, subset,
  6605. NULL, NULL, min, max));
  6606. }
  6607. /************************************************************************
  6608. * *
  6609. * Public API for operations on expressions *
  6610. * *
  6611. ************************************************************************/
  6612. static int
  6613. xmlExpGetLanguageInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
  6614. const xmlChar**list, int len, int nb) {
  6615. int tmp, tmp2;
  6616. tail:
  6617. switch (exp->type) {
  6618. case XML_EXP_EMPTY:
  6619. return(0);
  6620. case XML_EXP_ATOM:
  6621. for (tmp = 0;tmp < nb;tmp++)
  6622. if (list[tmp] == exp->exp_str)
  6623. return(0);
  6624. if (nb >= len)
  6625. return(-2);
  6626. list[nb] = exp->exp_str;
  6627. return(1);
  6628. case XML_EXP_COUNT:
  6629. exp = exp->exp_left;
  6630. goto tail;
  6631. case XML_EXP_SEQ:
  6632. case XML_EXP_OR:
  6633. tmp = xmlExpGetLanguageInt(ctxt, exp->exp_left, list, len, nb);
  6634. if (tmp < 0)
  6635. return(tmp);
  6636. tmp2 = xmlExpGetLanguageInt(ctxt, exp->exp_right, list, len,
  6637. nb + tmp);
  6638. if (tmp2 < 0)
  6639. return(tmp2);
  6640. return(tmp + tmp2);
  6641. }
  6642. return(-1);
  6643. }
  6644. /**
  6645. * xmlExpGetLanguage:
  6646. * @ctxt: the expression context
  6647. * @exp: the expression
  6648. * @langList: where to store the tokens
  6649. * @len: the allocated length of @list
  6650. *
  6651. * Find all the strings used in @exp and store them in @list
  6652. *
  6653. * Returns the number of unique strings found, -1 in case of errors and
  6654. * -2 if there is more than @len strings
  6655. */
  6656. int
  6657. xmlExpGetLanguage(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
  6658. const xmlChar**langList, int len) {
  6659. if ((ctxt == NULL) || (exp == NULL) || (langList == NULL) || (len <= 0))
  6660. return(-1);
  6661. return(xmlExpGetLanguageInt(ctxt, exp, langList, len, 0));
  6662. }
  6663. static int
  6664. xmlExpGetStartInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
  6665. const xmlChar**list, int len, int nb) {
  6666. int tmp, tmp2;
  6667. tail:
  6668. switch (exp->type) {
  6669. case XML_EXP_FORBID:
  6670. return(0);
  6671. case XML_EXP_EMPTY:
  6672. return(0);
  6673. case XML_EXP_ATOM:
  6674. for (tmp = 0;tmp < nb;tmp++)
  6675. if (list[tmp] == exp->exp_str)
  6676. return(0);
  6677. if (nb >= len)
  6678. return(-2);
  6679. list[nb] = exp->exp_str;
  6680. return(1);
  6681. case XML_EXP_COUNT:
  6682. exp = exp->exp_left;
  6683. goto tail;
  6684. case XML_EXP_SEQ:
  6685. tmp = xmlExpGetStartInt(ctxt, exp->exp_left, list, len, nb);
  6686. if (tmp < 0)
  6687. return(tmp);
  6688. if (IS_NILLABLE(exp->exp_left)) {
  6689. tmp2 = xmlExpGetStartInt(ctxt, exp->exp_right, list, len,
  6690. nb + tmp);
  6691. if (tmp2 < 0)
  6692. return(tmp2);
  6693. tmp += tmp2;
  6694. }
  6695. return(tmp);
  6696. case XML_EXP_OR:
  6697. tmp = xmlExpGetStartInt(ctxt, exp->exp_left, list, len, nb);
  6698. if (tmp < 0)
  6699. return(tmp);
  6700. tmp2 = xmlExpGetStartInt(ctxt, exp->exp_right, list, len,
  6701. nb + tmp);
  6702. if (tmp2 < 0)
  6703. return(tmp2);
  6704. return(tmp + tmp2);
  6705. }
  6706. return(-1);
  6707. }
  6708. /**
  6709. * xmlExpGetStart:
  6710. * @ctxt: the expression context
  6711. * @exp: the expression
  6712. * @tokList: where to store the tokens
  6713. * @len: the allocated length of @list
  6714. *
  6715. * Find all the strings that appears at the start of the languages
  6716. * accepted by @exp and store them in @list. E.g. for (a, b) | c
  6717. * it will return the list [a, c]
  6718. *
  6719. * Returns the number of unique strings found, -1 in case of errors and
  6720. * -2 if there is more than @len strings
  6721. */
  6722. int
  6723. xmlExpGetStart(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
  6724. const xmlChar**tokList, int len) {
  6725. if ((ctxt == NULL) || (exp == NULL) || (tokList == NULL) || (len <= 0))
  6726. return(-1);
  6727. return(xmlExpGetStartInt(ctxt, exp, tokList, len, 0));
  6728. }
  6729. /**
  6730. * xmlExpIsNillable:
  6731. * @exp: the expression
  6732. *
  6733. * Finds if the expression is nillable, i.e. if it accepts the empty sequence
  6734. *
  6735. * Returns 1 if nillable, 0 if not and -1 in case of error
  6736. */
  6737. int
  6738. xmlExpIsNillable(xmlExpNodePtr exp) {
  6739. if (exp == NULL)
  6740. return(-1);
  6741. return(IS_NILLABLE(exp) != 0);
  6742. }
  6743. static xmlExpNodePtr
  6744. xmlExpStringDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, const xmlChar *str)
  6745. {
  6746. xmlExpNodePtr ret;
  6747. switch (exp->type) {
  6748. case XML_EXP_EMPTY:
  6749. return(forbiddenExp);
  6750. case XML_EXP_FORBID:
  6751. return(forbiddenExp);
  6752. case XML_EXP_ATOM:
  6753. if (exp->exp_str == str) {
  6754. #ifdef DEBUG_DERIV
  6755. printf("deriv atom: equal => Empty\n");
  6756. #endif
  6757. ret = emptyExp;
  6758. } else {
  6759. #ifdef DEBUG_DERIV
  6760. printf("deriv atom: mismatch => forbid\n");
  6761. #endif
  6762. /* TODO wildcards here */
  6763. ret = forbiddenExp;
  6764. }
  6765. return(ret);
  6766. case XML_EXP_OR: {
  6767. xmlExpNodePtr tmp;
  6768. #ifdef DEBUG_DERIV
  6769. printf("deriv or: => or(derivs)\n");
  6770. #endif
  6771. tmp = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
  6772. if (tmp == NULL) {
  6773. return(NULL);
  6774. }
  6775. ret = xmlExpStringDeriveInt(ctxt, exp->exp_right, str);
  6776. if (ret == NULL) {
  6777. xmlExpFree(ctxt, tmp);
  6778. return(NULL);
  6779. }
  6780. ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, tmp, ret,
  6781. NULL, 0, 0);
  6782. return(ret);
  6783. }
  6784. case XML_EXP_SEQ:
  6785. #ifdef DEBUG_DERIV
  6786. printf("deriv seq: starting with left\n");
  6787. #endif
  6788. ret = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
  6789. if (ret == NULL) {
  6790. return(NULL);
  6791. } else if (ret == forbiddenExp) {
  6792. if (IS_NILLABLE(exp->exp_left)) {
  6793. #ifdef DEBUG_DERIV
  6794. printf("deriv seq: left failed but nillable\n");
  6795. #endif
  6796. ret = xmlExpStringDeriveInt(ctxt, exp->exp_right, str);
  6797. }
  6798. } else {
  6799. #ifdef DEBUG_DERIV
  6800. printf("deriv seq: left match => sequence\n");
  6801. #endif
  6802. exp->exp_right->ref++;
  6803. ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, exp->exp_right,
  6804. NULL, 0, 0);
  6805. }
  6806. return(ret);
  6807. case XML_EXP_COUNT: {
  6808. int min, max;
  6809. xmlExpNodePtr tmp;
  6810. if (exp->exp_max == 0)
  6811. return(forbiddenExp);
  6812. ret = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
  6813. if (ret == NULL)
  6814. return(NULL);
  6815. if (ret == forbiddenExp) {
  6816. #ifdef DEBUG_DERIV
  6817. printf("deriv count: pattern mismatch => forbid\n");
  6818. #endif
  6819. return(ret);
  6820. }
  6821. if (exp->exp_max == 1)
  6822. return(ret);
  6823. if (exp->exp_max < 0) /* unbounded */
  6824. max = -1;
  6825. else
  6826. max = exp->exp_max - 1;
  6827. if (exp->exp_min > 0)
  6828. min = exp->exp_min - 1;
  6829. else
  6830. min = 0;
  6831. exp->exp_left->ref++;
  6832. tmp = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left, NULL,
  6833. NULL, min, max);
  6834. if (ret == emptyExp) {
  6835. #ifdef DEBUG_DERIV
  6836. printf("deriv count: match to empty => new count\n");
  6837. #endif
  6838. return(tmp);
  6839. }
  6840. #ifdef DEBUG_DERIV
  6841. printf("deriv count: match => sequence with new count\n");
  6842. #endif
  6843. return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, tmp,
  6844. NULL, 0, 0));
  6845. }
  6846. }
  6847. return(NULL);
  6848. }
  6849. /**
  6850. * xmlExpStringDerive:
  6851. * @ctxt: the expression context
  6852. * @exp: the expression
  6853. * @str: the string
  6854. * @len: the string len in bytes if available
  6855. *
  6856. * Do one step of Brzozowski derivation of the expression @exp with
  6857. * respect to the input string
  6858. *
  6859. * Returns the resulting expression or NULL in case of internal error
  6860. */
  6861. xmlExpNodePtr
  6862. xmlExpStringDerive(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
  6863. const xmlChar *str, int len) {
  6864. const xmlChar *input;
  6865. if ((exp == NULL) || (ctxt == NULL) || (str == NULL)) {
  6866. return(NULL);
  6867. }
  6868. /*
  6869. * check the string is in the dictionary, if yes use an interned
  6870. * copy, otherwise we know it's not an acceptable input
  6871. */
  6872. input = xmlDictExists(ctxt->dict, str, len);
  6873. if (input == NULL) {
  6874. return(forbiddenExp);
  6875. }
  6876. return(xmlExpStringDeriveInt(ctxt, exp, input));
  6877. }
  6878. static int
  6879. xmlExpCheckCard(xmlExpNodePtr exp, xmlExpNodePtr sub) {
  6880. int ret = 1;
  6881. if (sub->c_max == -1) {
  6882. if (exp->c_max != -1)
  6883. ret = 0;
  6884. } else if ((exp->c_max >= 0) && (exp->c_max < sub->c_max)) {
  6885. ret = 0;
  6886. }
  6887. #if 0
  6888. if ((IS_NILLABLE(sub)) && (!IS_NILLABLE(exp)))
  6889. ret = 0;
  6890. #endif
  6891. return(ret);
  6892. }
  6893. static xmlExpNodePtr xmlExpExpDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
  6894. xmlExpNodePtr sub);
  6895. /**
  6896. * xmlExpDivide:
  6897. * @ctxt: the expressions context
  6898. * @exp: the englobing expression
  6899. * @sub: the subexpression
  6900. * @mult: the multiple expression
  6901. * @remain: the remain from the derivation of the multiple
  6902. *
  6903. * Check if exp is a multiple of sub, i.e. if there is a finite number n
  6904. * so that sub{n} subsume exp
  6905. *
  6906. * Returns the multiple value if successful, 0 if it is not a multiple
  6907. * and -1 in case of internal error.
  6908. */
  6909. static int
  6910. xmlExpDivide(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub,
  6911. xmlExpNodePtr *mult, xmlExpNodePtr *remain) {
  6912. int i;
  6913. xmlExpNodePtr tmp, tmp2;
  6914. if (mult != NULL) *mult = NULL;
  6915. if (remain != NULL) *remain = NULL;
  6916. if (exp->c_max == -1) return(0);
  6917. if (IS_NILLABLE(exp) && (!IS_NILLABLE(sub))) return(0);
  6918. for (i = 1;i <= exp->c_max;i++) {
  6919. sub->ref++;
  6920. tmp = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT,
  6921. sub, NULL, NULL, i, i);
  6922. if (tmp == NULL) {
  6923. return(-1);
  6924. }
  6925. if (!xmlExpCheckCard(tmp, exp)) {
  6926. xmlExpFree(ctxt, tmp);
  6927. continue;
  6928. }
  6929. tmp2 = xmlExpExpDeriveInt(ctxt, tmp, exp);
  6930. if (tmp2 == NULL) {
  6931. xmlExpFree(ctxt, tmp);
  6932. return(-1);
  6933. }
  6934. if ((tmp2 != forbiddenExp) && (IS_NILLABLE(tmp2))) {
  6935. if (remain != NULL)
  6936. *remain = tmp2;
  6937. else
  6938. xmlExpFree(ctxt, tmp2);
  6939. if (mult != NULL)
  6940. *mult = tmp;
  6941. else
  6942. xmlExpFree(ctxt, tmp);
  6943. #ifdef DEBUG_DERIV
  6944. printf("Divide succeeded %d\n", i);
  6945. #endif
  6946. return(i);
  6947. }
  6948. xmlExpFree(ctxt, tmp);
  6949. xmlExpFree(ctxt, tmp2);
  6950. }
  6951. #ifdef DEBUG_DERIV
  6952. printf("Divide failed\n");
  6953. #endif
  6954. return(0);
  6955. }
  6956. /**
  6957. * xmlExpExpDeriveInt:
  6958. * @ctxt: the expressions context
  6959. * @exp: the englobing expression
  6960. * @sub: the subexpression
  6961. *
  6962. * Try to do a step of Brzozowski derivation but at a higher level
  6963. * the input being a subexpression.
  6964. *
  6965. * Returns the resulting expression or NULL in case of internal error
  6966. */
  6967. static xmlExpNodePtr
  6968. xmlExpExpDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
  6969. xmlExpNodePtr ret, tmp, tmp2, tmp3;
  6970. const xmlChar **tab;
  6971. int len, i;
  6972. /*
  6973. * In case of equality and if the expression can only consume a finite
  6974. * amount, then the derivation is empty
  6975. */
  6976. if ((exp == sub) && (exp->c_max >= 0)) {
  6977. #ifdef DEBUG_DERIV
  6978. printf("Equal(exp, sub) and finite -> Empty\n");
  6979. #endif
  6980. return(emptyExp);
  6981. }
  6982. /*
  6983. * decompose sub sequence first
  6984. */
  6985. if (sub->type == XML_EXP_EMPTY) {
  6986. #ifdef DEBUG_DERIV
  6987. printf("Empty(sub) -> Empty\n");
  6988. #endif
  6989. exp->ref++;
  6990. return(exp);
  6991. }
  6992. if (sub->type == XML_EXP_SEQ) {
  6993. #ifdef DEBUG_DERIV
  6994. printf("Seq(sub) -> decompose\n");
  6995. #endif
  6996. tmp = xmlExpExpDeriveInt(ctxt, exp, sub->exp_left);
  6997. if (tmp == NULL)
  6998. return(NULL);
  6999. if (tmp == forbiddenExp)
  7000. return(tmp);
  7001. ret = xmlExpExpDeriveInt(ctxt, tmp, sub->exp_right);
  7002. xmlExpFree(ctxt, tmp);
  7003. return(ret);
  7004. }
  7005. if (sub->type == XML_EXP_OR) {
  7006. #ifdef DEBUG_DERIV
  7007. printf("Or(sub) -> decompose\n");
  7008. #endif
  7009. tmp = xmlExpExpDeriveInt(ctxt, exp, sub->exp_left);
  7010. if (tmp == forbiddenExp)
  7011. return(tmp);
  7012. if (tmp == NULL)
  7013. return(NULL);
  7014. ret = xmlExpExpDeriveInt(ctxt, exp, sub->exp_right);
  7015. if ((ret == NULL) || (ret == forbiddenExp)) {
  7016. xmlExpFree(ctxt, tmp);
  7017. return(ret);
  7018. }
  7019. return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, tmp, ret, NULL, 0, 0));
  7020. }
  7021. if (!xmlExpCheckCard(exp, sub)) {
  7022. #ifdef DEBUG_DERIV
  7023. printf("CheckCard(exp, sub) failed -> Forbid\n");
  7024. #endif
  7025. return(forbiddenExp);
  7026. }
  7027. switch (exp->type) {
  7028. case XML_EXP_EMPTY:
  7029. if (sub == emptyExp)
  7030. return(emptyExp);
  7031. #ifdef DEBUG_DERIV
  7032. printf("Empty(exp) -> Forbid\n");
  7033. #endif
  7034. return(forbiddenExp);
  7035. case XML_EXP_FORBID:
  7036. #ifdef DEBUG_DERIV
  7037. printf("Forbid(exp) -> Forbid\n");
  7038. #endif
  7039. return(forbiddenExp);
  7040. case XML_EXP_ATOM:
  7041. if (sub->type == XML_EXP_ATOM) {
  7042. /* TODO: handle wildcards */
  7043. if (exp->exp_str == sub->exp_str) {
  7044. #ifdef DEBUG_DERIV
  7045. printf("Atom match -> Empty\n");
  7046. #endif
  7047. return(emptyExp);
  7048. }
  7049. #ifdef DEBUG_DERIV
  7050. printf("Atom mismatch -> Forbid\n");
  7051. #endif
  7052. return(forbiddenExp);
  7053. }
  7054. if ((sub->type == XML_EXP_COUNT) &&
  7055. (sub->exp_max == 1) &&
  7056. (sub->exp_left->type == XML_EXP_ATOM)) {
  7057. /* TODO: handle wildcards */
  7058. if (exp->exp_str == sub->exp_left->exp_str) {
  7059. #ifdef DEBUG_DERIV
  7060. printf("Atom match -> Empty\n");
  7061. #endif
  7062. return(emptyExp);
  7063. }
  7064. #ifdef DEBUG_DERIV
  7065. printf("Atom mismatch -> Forbid\n");
  7066. #endif
  7067. return(forbiddenExp);
  7068. }
  7069. #ifdef DEBUG_DERIV
  7070. printf("Complex exp vs Atom -> Forbid\n");
  7071. #endif
  7072. return(forbiddenExp);
  7073. case XML_EXP_SEQ:
  7074. /* try to get the sequence consumed only if possible */
  7075. if (xmlExpCheckCard(exp->exp_left, sub)) {
  7076. /* See if the sequence can be consumed directly */
  7077. #ifdef DEBUG_DERIV
  7078. printf("Seq trying left only\n");
  7079. #endif
  7080. ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
  7081. if ((ret != forbiddenExp) && (ret != NULL)) {
  7082. #ifdef DEBUG_DERIV
  7083. printf("Seq trying left only worked\n");
  7084. #endif
  7085. /*
  7086. * TODO: assumption here that we are determinist
  7087. * i.e. we won't get to a nillable exp left
  7088. * subset which could be matched by the right
  7089. * part too.
  7090. * e.g.: (a | b)+,(a | c) and 'a+,a'
  7091. */
  7092. exp->exp_right->ref++;
  7093. return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret,
  7094. exp->exp_right, NULL, 0, 0));
  7095. }
  7096. #ifdef DEBUG_DERIV
  7097. } else {
  7098. printf("Seq: left too short\n");
  7099. #endif
  7100. }
  7101. /* Try instead to decompose */
  7102. if (sub->type == XML_EXP_COUNT) {
  7103. int min, max;
  7104. #ifdef DEBUG_DERIV
  7105. printf("Seq: sub is a count\n");
  7106. #endif
  7107. ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub->exp_left);
  7108. if (ret == NULL)
  7109. return(NULL);
  7110. if (ret != forbiddenExp) {
  7111. #ifdef DEBUG_DERIV
  7112. printf("Seq , Count match on left\n");
  7113. #endif
  7114. if (sub->exp_max < 0)
  7115. max = -1;
  7116. else
  7117. max = sub->exp_max -1;
  7118. if (sub->exp_min > 0)
  7119. min = sub->exp_min -1;
  7120. else
  7121. min = 0;
  7122. exp->exp_right->ref++;
  7123. tmp = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret,
  7124. exp->exp_right, NULL, 0, 0);
  7125. if (tmp == NULL)
  7126. return(NULL);
  7127. sub->exp_left->ref++;
  7128. tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT,
  7129. sub->exp_left, NULL, NULL, min, max);
  7130. if (tmp2 == NULL) {
  7131. xmlExpFree(ctxt, tmp);
  7132. return(NULL);
  7133. }
  7134. ret = xmlExpExpDeriveInt(ctxt, tmp, tmp2);
  7135. xmlExpFree(ctxt, tmp);
  7136. xmlExpFree(ctxt, tmp2);
  7137. return(ret);
  7138. }
  7139. }
  7140. /* we made no progress on structured operations */
  7141. break;
  7142. case XML_EXP_OR:
  7143. #ifdef DEBUG_DERIV
  7144. printf("Or , trying both side\n");
  7145. #endif
  7146. ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
  7147. if (ret == NULL)
  7148. return(NULL);
  7149. tmp = xmlExpExpDeriveInt(ctxt, exp->exp_right, sub);
  7150. if (tmp == NULL) {
  7151. xmlExpFree(ctxt, ret);
  7152. return(NULL);
  7153. }
  7154. return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, tmp, NULL, 0, 0));
  7155. case XML_EXP_COUNT: {
  7156. int min, max;
  7157. if (sub->type == XML_EXP_COUNT) {
  7158. /*
  7159. * Try to see if the loop is completely subsumed
  7160. */
  7161. tmp = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub->exp_left);
  7162. if (tmp == NULL)
  7163. return(NULL);
  7164. if (tmp == forbiddenExp) {
  7165. int mult;
  7166. #ifdef DEBUG_DERIV
  7167. printf("Count, Count inner don't subsume\n");
  7168. #endif
  7169. mult = xmlExpDivide(ctxt, sub->exp_left, exp->exp_left,
  7170. NULL, &tmp);
  7171. if (mult <= 0) {
  7172. #ifdef DEBUG_DERIV
  7173. printf("Count, Count not multiple => forbidden\n");
  7174. #endif
  7175. return(forbiddenExp);
  7176. }
  7177. if (sub->exp_max == -1) {
  7178. max = -1;
  7179. if (exp->exp_max == -1) {
  7180. if (exp->exp_min <= sub->exp_min * mult)
  7181. min = 0;
  7182. else
  7183. min = exp->exp_min - sub->exp_min * mult;
  7184. } else {
  7185. #ifdef DEBUG_DERIV
  7186. printf("Count, Count finite can't subsume infinite\n");
  7187. #endif
  7188. xmlExpFree(ctxt, tmp);
  7189. return(forbiddenExp);
  7190. }
  7191. } else {
  7192. if (exp->exp_max == -1) {
  7193. #ifdef DEBUG_DERIV
  7194. printf("Infinite loop consume mult finite loop\n");
  7195. #endif
  7196. if (exp->exp_min > sub->exp_min * mult) {
  7197. max = -1;
  7198. min = exp->exp_min - sub->exp_min * mult;
  7199. } else {
  7200. max = -1;
  7201. min = 0;
  7202. }
  7203. } else {
  7204. if (exp->exp_max < sub->exp_max * mult) {
  7205. #ifdef DEBUG_DERIV
  7206. printf("loops max mult mismatch => forbidden\n");
  7207. #endif
  7208. xmlExpFree(ctxt, tmp);
  7209. return(forbiddenExp);
  7210. }
  7211. if (sub->exp_max * mult > exp->exp_min)
  7212. min = 0;
  7213. else
  7214. min = exp->exp_min - sub->exp_max * mult;
  7215. max = exp->exp_max - sub->exp_max * mult;
  7216. }
  7217. }
  7218. } else if (!IS_NILLABLE(tmp)) {
  7219. /*
  7220. * TODO: loop here to try to grow if working on finite
  7221. * blocks.
  7222. */
  7223. #ifdef DEBUG_DERIV
  7224. printf("Count, Count remain not nillable => forbidden\n");
  7225. #endif
  7226. xmlExpFree(ctxt, tmp);
  7227. return(forbiddenExp);
  7228. } else if (sub->exp_max == -1) {
  7229. if (exp->exp_max == -1) {
  7230. if (exp->exp_min <= sub->exp_min) {
  7231. #ifdef DEBUG_DERIV
  7232. printf("Infinite loops Okay => COUNT(0,Inf)\n");
  7233. #endif
  7234. max = -1;
  7235. min = 0;
  7236. } else {
  7237. #ifdef DEBUG_DERIV
  7238. printf("Infinite loops min => Count(X,Inf)\n");
  7239. #endif
  7240. max = -1;
  7241. min = exp->exp_min - sub->exp_min;
  7242. }
  7243. } else if (exp->exp_min > sub->exp_min) {
  7244. #ifdef DEBUG_DERIV
  7245. printf("loops min mismatch 1 => forbidden ???\n");
  7246. #endif
  7247. xmlExpFree(ctxt, tmp);
  7248. return(forbiddenExp);
  7249. } else {
  7250. max = -1;
  7251. min = 0;
  7252. }
  7253. } else {
  7254. if (exp->exp_max == -1) {
  7255. #ifdef DEBUG_DERIV
  7256. printf("Infinite loop consume finite loop\n");
  7257. #endif
  7258. if (exp->exp_min > sub->exp_min) {
  7259. max = -1;
  7260. min = exp->exp_min - sub->exp_min;
  7261. } else {
  7262. max = -1;
  7263. min = 0;
  7264. }
  7265. } else {
  7266. if (exp->exp_max < sub->exp_max) {
  7267. #ifdef DEBUG_DERIV
  7268. printf("loops max mismatch => forbidden\n");
  7269. #endif
  7270. xmlExpFree(ctxt, tmp);
  7271. return(forbiddenExp);
  7272. }
  7273. if (sub->exp_max > exp->exp_min)
  7274. min = 0;
  7275. else
  7276. min = exp->exp_min - sub->exp_max;
  7277. max = exp->exp_max - sub->exp_max;
  7278. }
  7279. }
  7280. #ifdef DEBUG_DERIV
  7281. printf("loops match => SEQ(COUNT())\n");
  7282. #endif
  7283. exp->exp_left->ref++;
  7284. tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left,
  7285. NULL, NULL, min, max);
  7286. if (tmp2 == NULL) {
  7287. return(NULL);
  7288. }
  7289. ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, tmp, tmp2,
  7290. NULL, 0, 0);
  7291. return(ret);
  7292. }
  7293. tmp = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
  7294. if (tmp == NULL)
  7295. return(NULL);
  7296. if (tmp == forbiddenExp) {
  7297. #ifdef DEBUG_DERIV
  7298. printf("loop mismatch => forbidden\n");
  7299. #endif
  7300. return(forbiddenExp);
  7301. }
  7302. if (exp->exp_min > 0)
  7303. min = exp->exp_min - 1;
  7304. else
  7305. min = 0;
  7306. if (exp->exp_max < 0)
  7307. max = -1;
  7308. else
  7309. max = exp->exp_max - 1;
  7310. #ifdef DEBUG_DERIV
  7311. printf("loop match => SEQ(COUNT())\n");
  7312. #endif
  7313. exp->exp_left->ref++;
  7314. tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left,
  7315. NULL, NULL, min, max);
  7316. if (tmp2 == NULL)
  7317. return(NULL);
  7318. ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, tmp, tmp2,
  7319. NULL, 0, 0);
  7320. return(ret);
  7321. }
  7322. }
  7323. #ifdef DEBUG_DERIV
  7324. printf("Fallback to derivative\n");
  7325. #endif
  7326. if (IS_NILLABLE(sub)) {
  7327. if (!(IS_NILLABLE(exp)))
  7328. return(forbiddenExp);
  7329. else
  7330. ret = emptyExp;
  7331. } else
  7332. ret = NULL;
  7333. /*
  7334. * here the structured derivation made no progress so
  7335. * we use the default token based derivation to force one more step
  7336. */
  7337. if (ctxt->tabSize == 0)
  7338. ctxt->tabSize = 40;
  7339. tab = (const xmlChar **) xmlMalloc(ctxt->tabSize *
  7340. sizeof(const xmlChar *));
  7341. if (tab == NULL) {
  7342. return(NULL);
  7343. }
  7344. /*
  7345. * collect all the strings accepted by the subexpression on input
  7346. */
  7347. len = xmlExpGetStartInt(ctxt, sub, tab, ctxt->tabSize, 0);
  7348. while (len < 0) {
  7349. const xmlChar **temp;
  7350. temp = (const xmlChar **) xmlRealloc((xmlChar **) tab, ctxt->tabSize * 2 *
  7351. sizeof(const xmlChar *));
  7352. if (temp == NULL) {
  7353. xmlFree((xmlChar **) tab);
  7354. return(NULL);
  7355. }
  7356. tab = temp;
  7357. ctxt->tabSize *= 2;
  7358. len = xmlExpGetStartInt(ctxt, sub, tab, ctxt->tabSize, 0);
  7359. }
  7360. for (i = 0;i < len;i++) {
  7361. tmp = xmlExpStringDeriveInt(ctxt, exp, tab[i]);
  7362. if ((tmp == NULL) || (tmp == forbiddenExp)) {
  7363. xmlExpFree(ctxt, ret);
  7364. xmlFree((xmlChar **) tab);
  7365. return(tmp);
  7366. }
  7367. tmp2 = xmlExpStringDeriveInt(ctxt, sub, tab[i]);
  7368. if ((tmp2 == NULL) || (tmp2 == forbiddenExp)) {
  7369. xmlExpFree(ctxt, tmp);
  7370. xmlExpFree(ctxt, ret);
  7371. xmlFree((xmlChar **) tab);
  7372. return(tmp);
  7373. }
  7374. tmp3 = xmlExpExpDeriveInt(ctxt, tmp, tmp2);
  7375. xmlExpFree(ctxt, tmp);
  7376. xmlExpFree(ctxt, tmp2);
  7377. if ((tmp3 == NULL) || (tmp3 == forbiddenExp)) {
  7378. xmlExpFree(ctxt, ret);
  7379. xmlFree((xmlChar **) tab);
  7380. return(tmp3);
  7381. }
  7382. if (ret == NULL)
  7383. ret = tmp3;
  7384. else {
  7385. ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, tmp3, NULL, 0, 0);
  7386. if (ret == NULL) {
  7387. xmlFree((xmlChar **) tab);
  7388. return(NULL);
  7389. }
  7390. }
  7391. }
  7392. xmlFree((xmlChar **) tab);
  7393. return(ret);
  7394. }
  7395. /**
  7396. * xmlExpExpDerive:
  7397. * @ctxt: the expressions context
  7398. * @exp: the englobing expression
  7399. * @sub: the subexpression
  7400. *
  7401. * Evaluates the expression resulting from @exp consuming a sub expression @sub
  7402. * Based on algebraic derivation and sometimes direct Brzozowski derivation
  7403. * it usually takes less than linear time and can handle expressions generating
  7404. * infinite languages.
  7405. *
  7406. * Returns the resulting expression or NULL in case of internal error, the
  7407. * result must be freed
  7408. */
  7409. xmlExpNodePtr
  7410. xmlExpExpDerive(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
  7411. if ((exp == NULL) || (ctxt == NULL) || (sub == NULL))
  7412. return(NULL);
  7413. /*
  7414. * O(1) speedups
  7415. */
  7416. if (IS_NILLABLE(sub) && (!IS_NILLABLE(exp))) {
  7417. #ifdef DEBUG_DERIV
  7418. printf("Sub nillable and not exp : can't subsume\n");
  7419. #endif
  7420. return(forbiddenExp);
  7421. }
  7422. if (xmlExpCheckCard(exp, sub) == 0) {
  7423. #ifdef DEBUG_DERIV
  7424. printf("sub generate longer sequences than exp : can't subsume\n");
  7425. #endif
  7426. return(forbiddenExp);
  7427. }
  7428. return(xmlExpExpDeriveInt(ctxt, exp, sub));
  7429. }
  7430. /**
  7431. * xmlExpSubsume:
  7432. * @ctxt: the expressions context
  7433. * @exp: the englobing expression
  7434. * @sub: the subexpression
  7435. *
  7436. * Check whether @exp accepts all the languages accepted by @sub
  7437. * the input being a subexpression.
  7438. *
  7439. * Returns 1 if true 0 if false and -1 in case of failure.
  7440. */
  7441. int
  7442. xmlExpSubsume(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
  7443. xmlExpNodePtr tmp;
  7444. if ((exp == NULL) || (ctxt == NULL) || (sub == NULL))
  7445. return(-1);
  7446. /*
  7447. * TODO: speedup by checking the language of sub is a subset of the
  7448. * language of exp
  7449. */
  7450. /*
  7451. * O(1) speedups
  7452. */
  7453. if (IS_NILLABLE(sub) && (!IS_NILLABLE(exp))) {
  7454. #ifdef DEBUG_DERIV
  7455. printf("Sub nillable and not exp : can't subsume\n");
  7456. #endif
  7457. return(0);
  7458. }
  7459. if (xmlExpCheckCard(exp, sub) == 0) {
  7460. #ifdef DEBUG_DERIV
  7461. printf("sub generate longer sequences than exp : can't subsume\n");
  7462. #endif
  7463. return(0);
  7464. }
  7465. tmp = xmlExpExpDeriveInt(ctxt, exp, sub);
  7466. #ifdef DEBUG_DERIV
  7467. printf("Result derivation :\n");
  7468. PRINT_EXP(tmp);
  7469. #endif
  7470. if (tmp == NULL)
  7471. return(-1);
  7472. if (tmp == forbiddenExp)
  7473. return(0);
  7474. if (tmp == emptyExp)
  7475. return(1);
  7476. if ((tmp != NULL) && (IS_NILLABLE(tmp))) {
  7477. xmlExpFree(ctxt, tmp);
  7478. return(1);
  7479. }
  7480. xmlExpFree(ctxt, tmp);
  7481. return(0);
  7482. }
  7483. /************************************************************************
  7484. * *
  7485. * Parsing expression *
  7486. * *
  7487. ************************************************************************/
  7488. static xmlExpNodePtr xmlExpParseExpr(xmlExpCtxtPtr ctxt);
  7489. #undef CUR
  7490. #define CUR (*ctxt->cur)
  7491. #undef NEXT
  7492. #define NEXT ctxt->cur++;
  7493. #undef IS_BLANK
  7494. #define IS_BLANK(c) ((c == ' ') || (c == '\n') || (c == '\r') || (c == '\t'))
  7495. #define SKIP_BLANKS while (IS_BLANK(*ctxt->cur)) ctxt->cur++;
  7496. static int
  7497. xmlExpParseNumber(xmlExpCtxtPtr ctxt) {
  7498. int ret = 0;
  7499. SKIP_BLANKS
  7500. if (CUR == '*') {
  7501. NEXT
  7502. return(-1);
  7503. }
  7504. if ((CUR < '0') || (CUR > '9'))
  7505. return(-1);
  7506. while ((CUR >= '0') && (CUR <= '9')) {
  7507. ret = ret * 10 + (CUR - '0');
  7508. NEXT
  7509. }
  7510. return(ret);
  7511. }
  7512. static xmlExpNodePtr
  7513. xmlExpParseOr(xmlExpCtxtPtr ctxt) {
  7514. const char *base;
  7515. xmlExpNodePtr ret;
  7516. const xmlChar *val;
  7517. SKIP_BLANKS
  7518. base = ctxt->cur;
  7519. if (*ctxt->cur == '(') {
  7520. NEXT
  7521. ret = xmlExpParseExpr(ctxt);
  7522. SKIP_BLANKS
  7523. if (*ctxt->cur != ')') {
  7524. fprintf(stderr, "unbalanced '(' : %s\n", base);
  7525. xmlExpFree(ctxt, ret);
  7526. return(NULL);
  7527. }
  7528. NEXT;
  7529. SKIP_BLANKS
  7530. goto parse_quantifier;
  7531. }
  7532. while ((CUR != 0) && (!(IS_BLANK(CUR))) && (CUR != '(') &&
  7533. (CUR != ')') && (CUR != '|') && (CUR != ',') && (CUR != '{') &&
  7534. (CUR != '*') && (CUR != '+') && (CUR != '?') && (CUR != '}'))
  7535. NEXT;
  7536. val = xmlDictLookup(ctxt->dict, BAD_CAST base, ctxt->cur - base);
  7537. if (val == NULL)
  7538. return(NULL);
  7539. ret = xmlExpHashGetEntry(ctxt, XML_EXP_ATOM, NULL, NULL, val, 0, 0);
  7540. if (ret == NULL)
  7541. return(NULL);
  7542. SKIP_BLANKS
  7543. parse_quantifier:
  7544. if (CUR == '{') {
  7545. int min, max;
  7546. NEXT
  7547. min = xmlExpParseNumber(ctxt);
  7548. if (min < 0) {
  7549. xmlExpFree(ctxt, ret);
  7550. return(NULL);
  7551. }
  7552. SKIP_BLANKS
  7553. if (CUR == ',') {
  7554. NEXT
  7555. max = xmlExpParseNumber(ctxt);
  7556. SKIP_BLANKS
  7557. } else
  7558. max = min;
  7559. if (CUR != '}') {
  7560. xmlExpFree(ctxt, ret);
  7561. return(NULL);
  7562. }
  7563. NEXT
  7564. ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
  7565. min, max);
  7566. SKIP_BLANKS
  7567. } else if (CUR == '?') {
  7568. NEXT
  7569. ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
  7570. 0, 1);
  7571. SKIP_BLANKS
  7572. } else if (CUR == '+') {
  7573. NEXT
  7574. ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
  7575. 1, -1);
  7576. SKIP_BLANKS
  7577. } else if (CUR == '*') {
  7578. NEXT
  7579. ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
  7580. 0, -1);
  7581. SKIP_BLANKS
  7582. }
  7583. return(ret);
  7584. }
  7585. static xmlExpNodePtr
  7586. xmlExpParseSeq(xmlExpCtxtPtr ctxt) {
  7587. xmlExpNodePtr ret, right;
  7588. ret = xmlExpParseOr(ctxt);
  7589. SKIP_BLANKS
  7590. while (CUR == '|') {
  7591. NEXT
  7592. right = xmlExpParseOr(ctxt);
  7593. if (right == NULL) {
  7594. xmlExpFree(ctxt, ret);
  7595. return(NULL);
  7596. }
  7597. ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, right, NULL, 0, 0);
  7598. if (ret == NULL)
  7599. return(NULL);
  7600. }
  7601. return(ret);
  7602. }
  7603. static xmlExpNodePtr
  7604. xmlExpParseExpr(xmlExpCtxtPtr ctxt) {
  7605. xmlExpNodePtr ret, right;
  7606. ret = xmlExpParseSeq(ctxt);
  7607. SKIP_BLANKS
  7608. while (CUR == ',') {
  7609. NEXT
  7610. right = xmlExpParseSeq(ctxt);
  7611. if (right == NULL) {
  7612. xmlExpFree(ctxt, ret);
  7613. return(NULL);
  7614. }
  7615. ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, right, NULL, 0, 0);
  7616. if (ret == NULL)
  7617. return(NULL);
  7618. }
  7619. return(ret);
  7620. }
  7621. /**
  7622. * xmlExpParse:
  7623. * @ctxt: the expressions context
  7624. * @expr: the 0 terminated string
  7625. *
  7626. * Minimal parser for regexps, it understand the following constructs
  7627. * - string terminals
  7628. * - choice operator |
  7629. * - sequence operator ,
  7630. * - subexpressions (...)
  7631. * - usual cardinality operators + * and ?
  7632. * - finite sequences { min, max }
  7633. * - infinite sequences { min, * }
  7634. * There is minimal checkings made especially no checking on strings values
  7635. *
  7636. * Returns a new expression or NULL in case of failure
  7637. */
  7638. xmlExpNodePtr
  7639. xmlExpParse(xmlExpCtxtPtr ctxt, const char *expr) {
  7640. xmlExpNodePtr ret;
  7641. ctxt->expr = expr;
  7642. ctxt->cur = expr;
  7643. ret = xmlExpParseExpr(ctxt);
  7644. SKIP_BLANKS
  7645. if (*ctxt->cur != 0) {
  7646. xmlExpFree(ctxt, ret);
  7647. return(NULL);
  7648. }
  7649. return(ret);
  7650. }
  7651. static void
  7652. xmlExpDumpInt(xmlBufferPtr buf, xmlExpNodePtr expr, int glob) {
  7653. xmlExpNodePtr c;
  7654. if (expr == NULL) return;
  7655. if (glob) xmlBufferWriteChar(buf, "(");
  7656. switch (expr->type) {
  7657. case XML_EXP_EMPTY:
  7658. xmlBufferWriteChar(buf, "empty");
  7659. break;
  7660. case XML_EXP_FORBID:
  7661. xmlBufferWriteChar(buf, "forbidden");
  7662. break;
  7663. case XML_EXP_ATOM:
  7664. xmlBufferWriteCHAR(buf, expr->exp_str);
  7665. break;
  7666. case XML_EXP_SEQ:
  7667. c = expr->exp_left;
  7668. if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
  7669. xmlExpDumpInt(buf, c, 1);
  7670. else
  7671. xmlExpDumpInt(buf, c, 0);
  7672. xmlBufferWriteChar(buf, " , ");
  7673. c = expr->exp_right;
  7674. if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
  7675. xmlExpDumpInt(buf, c, 1);
  7676. else
  7677. xmlExpDumpInt(buf, c, 0);
  7678. break;
  7679. case XML_EXP_OR:
  7680. c = expr->exp_left;
  7681. if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
  7682. xmlExpDumpInt(buf, c, 1);
  7683. else
  7684. xmlExpDumpInt(buf, c, 0);
  7685. xmlBufferWriteChar(buf, " | ");
  7686. c = expr->exp_right;
  7687. if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
  7688. xmlExpDumpInt(buf, c, 1);
  7689. else
  7690. xmlExpDumpInt(buf, c, 0);
  7691. break;
  7692. case XML_EXP_COUNT: {
  7693. char rep[40];
  7694. c = expr->exp_left;
  7695. if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
  7696. xmlExpDumpInt(buf, c, 1);
  7697. else
  7698. xmlExpDumpInt(buf, c, 0);
  7699. if ((expr->exp_min == 0) && (expr->exp_max == 1)) {
  7700. rep[0] = '?';
  7701. rep[1] = 0;
  7702. } else if ((expr->exp_min == 0) && (expr->exp_max == -1)) {
  7703. rep[0] = '*';
  7704. rep[1] = 0;
  7705. } else if ((expr->exp_min == 1) && (expr->exp_max == -1)) {
  7706. rep[0] = '+';
  7707. rep[1] = 0;
  7708. } else if (expr->exp_max == expr->exp_min) {
  7709. snprintf(rep, 39, "{%d}", expr->exp_min);
  7710. } else if (expr->exp_max < 0) {
  7711. snprintf(rep, 39, "{%d,inf}", expr->exp_min);
  7712. } else {
  7713. snprintf(rep, 39, "{%d,%d}", expr->exp_min, expr->exp_max);
  7714. }
  7715. rep[39] = 0;
  7716. xmlBufferWriteChar(buf, rep);
  7717. break;
  7718. }
  7719. default:
  7720. fprintf(stderr, "Error in tree\n");
  7721. }
  7722. if (glob)
  7723. xmlBufferWriteChar(buf, ")");
  7724. }
  7725. /**
  7726. * xmlExpDump:
  7727. * @buf: a buffer to receive the output
  7728. * @expr: the compiled expression
  7729. *
  7730. * Serialize the expression as compiled to the buffer
  7731. */
  7732. void
  7733. xmlExpDump(xmlBufferPtr buf, xmlExpNodePtr expr) {
  7734. if ((buf == NULL) || (expr == NULL))
  7735. return;
  7736. xmlExpDumpInt(buf, expr, 0);
  7737. }
  7738. /**
  7739. * xmlExpMaxToken:
  7740. * @expr: a compiled expression
  7741. *
  7742. * Indicate the maximum number of input a expression can accept
  7743. *
  7744. * Returns the maximum length or -1 in case of error
  7745. */
  7746. int
  7747. xmlExpMaxToken(xmlExpNodePtr expr) {
  7748. if (expr == NULL)
  7749. return(-1);
  7750. return(expr->c_max);
  7751. }
  7752. /**
  7753. * xmlExpCtxtNbNodes:
  7754. * @ctxt: an expression context
  7755. *
  7756. * Debugging facility provides the number of allocated nodes at a that point
  7757. *
  7758. * Returns the number of nodes in use or -1 in case of error
  7759. */
  7760. int
  7761. xmlExpCtxtNbNodes(xmlExpCtxtPtr ctxt) {
  7762. if (ctxt == NULL)
  7763. return(-1);
  7764. return(ctxt->nb_nodes);
  7765. }
  7766. /**
  7767. * xmlExpCtxtNbCons:
  7768. * @ctxt: an expression context
  7769. *
  7770. * Debugging facility provides the number of allocated nodes over lifetime
  7771. *
  7772. * Returns the number of nodes ever allocated or -1 in case of error
  7773. */
  7774. int
  7775. xmlExpCtxtNbCons(xmlExpCtxtPtr ctxt) {
  7776. if (ctxt == NULL)
  7777. return(-1);
  7778. return(ctxt->nb_cons);
  7779. }
  7780. #endif /* LIBXML_EXPR_ENABLED */
  7781. #define bottom_xmlregexp
  7782. #include "elfgcchack.h"
  7783. #endif /* LIBXML_REGEXP_ENABLED */