xmlregexp.c 212 KB

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