xmlschemastypes.c 176 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276
  1. /*
  2. * schemastypes.c : implementation of the XML Schema Datatypes
  3. * definition and validity checking
  4. *
  5. * See Copyright for the status of this software.
  6. *
  7. * Daniel Veillard <veillard@redhat.com>
  8. */
  9. /* To avoid EBCDIC trouble when parsing on zOS */
  10. #if defined(__MVS__)
  11. #pragma convert("ISO8859-1")
  12. #endif
  13. #define IN_LIBXML
  14. #include "libxml.h"
  15. #ifdef LIBXML_SCHEMAS_ENABLED
  16. #include <string.h>
  17. #include <libxml/xmlmemory.h>
  18. #include <libxml/parser.h>
  19. #include <libxml/parserInternals.h>
  20. #include <libxml/hash.h>
  21. #include <libxml/valid.h>
  22. #include <libxml/xpath.h>
  23. #include <libxml/uri.h>
  24. #include <libxml/xmlschemas.h>
  25. #include <libxml/schemasInternals.h>
  26. #include <libxml/xmlschemastypes.h>
  27. #ifdef HAVE_MATH_H
  28. #include <math.h>
  29. #endif
  30. #ifdef HAVE_FLOAT_H
  31. #include <float.h>
  32. #endif
  33. #define DEBUG
  34. #ifndef LIBXML_XPATH_ENABLED
  35. extern double xmlXPathNAN;
  36. extern double xmlXPathPINF;
  37. extern double xmlXPathNINF;
  38. #endif
  39. #define TODO \
  40. xmlGenericError(xmlGenericErrorContext, \
  41. "Unimplemented block at %s:%d\n", \
  42. __FILE__, __LINE__);
  43. #define XML_SCHEMAS_NAMESPACE_NAME \
  44. (const xmlChar *)"http://www.w3.org/2001/XMLSchema"
  45. #define IS_WSP_REPLACE_CH(c) ((((c) == 0x9) || ((c) == 0xa)) || \
  46. ((c) == 0xd))
  47. #define IS_WSP_SPACE_CH(c) ((c) == 0x20)
  48. #define IS_WSP_BLANK_CH(c) IS_BLANK_CH(c)
  49. /* Date value */
  50. typedef struct _xmlSchemaValDate xmlSchemaValDate;
  51. typedef xmlSchemaValDate *xmlSchemaValDatePtr;
  52. struct _xmlSchemaValDate {
  53. long year;
  54. unsigned int mon :4; /* 1 <= mon <= 12 */
  55. unsigned int day :5; /* 1 <= day <= 31 */
  56. unsigned int hour :5; /* 0 <= hour <= 24 */
  57. unsigned int min :6; /* 0 <= min <= 59 */
  58. double sec;
  59. unsigned int tz_flag :1; /* is tzo explicitly set? */
  60. signed int tzo :12; /* -1440 <= tzo <= 1440;
  61. currently only -840 to +840 are needed */
  62. };
  63. /* Duration value */
  64. typedef struct _xmlSchemaValDuration xmlSchemaValDuration;
  65. typedef xmlSchemaValDuration *xmlSchemaValDurationPtr;
  66. struct _xmlSchemaValDuration {
  67. long mon; /* mon stores years also */
  68. long day;
  69. double sec; /* sec stores min and hour also */
  70. };
  71. typedef struct _xmlSchemaValDecimal xmlSchemaValDecimal;
  72. typedef xmlSchemaValDecimal *xmlSchemaValDecimalPtr;
  73. struct _xmlSchemaValDecimal {
  74. /* would use long long but not portable */
  75. unsigned long lo;
  76. unsigned long mi;
  77. unsigned long hi;
  78. unsigned int extra;
  79. unsigned int sign:1;
  80. unsigned int frac:7;
  81. unsigned int total:8;
  82. };
  83. typedef struct _xmlSchemaValQName xmlSchemaValQName;
  84. typedef xmlSchemaValQName *xmlSchemaValQNamePtr;
  85. struct _xmlSchemaValQName {
  86. xmlChar *name;
  87. xmlChar *uri;
  88. };
  89. typedef struct _xmlSchemaValHex xmlSchemaValHex;
  90. typedef xmlSchemaValHex *xmlSchemaValHexPtr;
  91. struct _xmlSchemaValHex {
  92. xmlChar *str;
  93. unsigned int total;
  94. };
  95. typedef struct _xmlSchemaValBase64 xmlSchemaValBase64;
  96. typedef xmlSchemaValBase64 *xmlSchemaValBase64Ptr;
  97. struct _xmlSchemaValBase64 {
  98. xmlChar *str;
  99. unsigned int total;
  100. };
  101. struct _xmlSchemaVal {
  102. xmlSchemaValType type;
  103. struct _xmlSchemaVal *next;
  104. union {
  105. xmlSchemaValDecimal decimal;
  106. xmlSchemaValDate date;
  107. xmlSchemaValDuration dur;
  108. xmlSchemaValQName qname;
  109. xmlSchemaValHex hex;
  110. xmlSchemaValBase64 base64;
  111. float f;
  112. double d;
  113. int b;
  114. xmlChar *str;
  115. } value;
  116. };
  117. static int xmlSchemaTypesInitialized = 0;
  118. static xmlHashTablePtr xmlSchemaTypesBank = NULL;
  119. /*
  120. * Basic types
  121. */
  122. static xmlSchemaTypePtr xmlSchemaTypeStringDef = NULL;
  123. static xmlSchemaTypePtr xmlSchemaTypeAnyTypeDef = NULL;
  124. static xmlSchemaTypePtr xmlSchemaTypeAnySimpleTypeDef = NULL;
  125. static xmlSchemaTypePtr xmlSchemaTypeDecimalDef = NULL;
  126. static xmlSchemaTypePtr xmlSchemaTypeDatetimeDef = NULL;
  127. static xmlSchemaTypePtr xmlSchemaTypeDateDef = NULL;
  128. static xmlSchemaTypePtr xmlSchemaTypeTimeDef = NULL;
  129. static xmlSchemaTypePtr xmlSchemaTypeGYearDef = NULL;
  130. static xmlSchemaTypePtr xmlSchemaTypeGYearMonthDef = NULL;
  131. static xmlSchemaTypePtr xmlSchemaTypeGDayDef = NULL;
  132. static xmlSchemaTypePtr xmlSchemaTypeGMonthDayDef = NULL;
  133. static xmlSchemaTypePtr xmlSchemaTypeGMonthDef = NULL;
  134. static xmlSchemaTypePtr xmlSchemaTypeDurationDef = NULL;
  135. static xmlSchemaTypePtr xmlSchemaTypeFloatDef = NULL;
  136. static xmlSchemaTypePtr xmlSchemaTypeBooleanDef = NULL;
  137. static xmlSchemaTypePtr xmlSchemaTypeDoubleDef = NULL;
  138. static xmlSchemaTypePtr xmlSchemaTypeHexBinaryDef = NULL;
  139. static xmlSchemaTypePtr xmlSchemaTypeBase64BinaryDef = NULL;
  140. static xmlSchemaTypePtr xmlSchemaTypeAnyURIDef = NULL;
  141. /*
  142. * Derived types
  143. */
  144. static xmlSchemaTypePtr xmlSchemaTypePositiveIntegerDef = NULL;
  145. static xmlSchemaTypePtr xmlSchemaTypeNonPositiveIntegerDef = NULL;
  146. static xmlSchemaTypePtr xmlSchemaTypeNegativeIntegerDef = NULL;
  147. static xmlSchemaTypePtr xmlSchemaTypeNonNegativeIntegerDef = NULL;
  148. static xmlSchemaTypePtr xmlSchemaTypeIntegerDef = NULL;
  149. static xmlSchemaTypePtr xmlSchemaTypeLongDef = NULL;
  150. static xmlSchemaTypePtr xmlSchemaTypeIntDef = NULL;
  151. static xmlSchemaTypePtr xmlSchemaTypeShortDef = NULL;
  152. static xmlSchemaTypePtr xmlSchemaTypeByteDef = NULL;
  153. static xmlSchemaTypePtr xmlSchemaTypeUnsignedLongDef = NULL;
  154. static xmlSchemaTypePtr xmlSchemaTypeUnsignedIntDef = NULL;
  155. static xmlSchemaTypePtr xmlSchemaTypeUnsignedShortDef = NULL;
  156. static xmlSchemaTypePtr xmlSchemaTypeUnsignedByteDef = NULL;
  157. static xmlSchemaTypePtr xmlSchemaTypeNormStringDef = NULL;
  158. static xmlSchemaTypePtr xmlSchemaTypeTokenDef = NULL;
  159. static xmlSchemaTypePtr xmlSchemaTypeLanguageDef = NULL;
  160. static xmlSchemaTypePtr xmlSchemaTypeNameDef = NULL;
  161. static xmlSchemaTypePtr xmlSchemaTypeQNameDef = NULL;
  162. static xmlSchemaTypePtr xmlSchemaTypeNCNameDef = NULL;
  163. static xmlSchemaTypePtr xmlSchemaTypeIdDef = NULL;
  164. static xmlSchemaTypePtr xmlSchemaTypeIdrefDef = NULL;
  165. static xmlSchemaTypePtr xmlSchemaTypeIdrefsDef = NULL;
  166. static xmlSchemaTypePtr xmlSchemaTypeEntityDef = NULL;
  167. static xmlSchemaTypePtr xmlSchemaTypeEntitiesDef = NULL;
  168. static xmlSchemaTypePtr xmlSchemaTypeNotationDef = NULL;
  169. static xmlSchemaTypePtr xmlSchemaTypeNmtokenDef = NULL;
  170. static xmlSchemaTypePtr xmlSchemaTypeNmtokensDef = NULL;
  171. /************************************************************************
  172. * *
  173. * Datatype error handlers *
  174. * *
  175. ************************************************************************/
  176. /**
  177. * xmlSchemaTypeErrMemory:
  178. * @extra: extra information
  179. *
  180. * Handle an out of memory condition
  181. */
  182. static void
  183. xmlSchemaTypeErrMemory(xmlNodePtr node, const char *extra)
  184. {
  185. __xmlSimpleError(XML_FROM_DATATYPE, XML_ERR_NO_MEMORY, node, NULL, extra);
  186. }
  187. /************************************************************************
  188. * *
  189. * Base types support *
  190. * *
  191. ************************************************************************/
  192. /**
  193. * xmlSchemaNewValue:
  194. * @type: the value type
  195. *
  196. * Allocate a new simple type value
  197. *
  198. * Returns a pointer to the new value or NULL in case of error
  199. */
  200. static xmlSchemaValPtr
  201. xmlSchemaNewValue(xmlSchemaValType type) {
  202. xmlSchemaValPtr value;
  203. value = (xmlSchemaValPtr) xmlMalloc(sizeof(xmlSchemaVal));
  204. if (value == NULL) {
  205. return(NULL);
  206. }
  207. memset(value, 0, sizeof(xmlSchemaVal));
  208. value->type = type;
  209. return(value);
  210. }
  211. static xmlSchemaFacetPtr
  212. xmlSchemaNewMinLengthFacet(int value)
  213. {
  214. xmlSchemaFacetPtr ret;
  215. ret = xmlSchemaNewFacet();
  216. if (ret == NULL) {
  217. return(NULL);
  218. }
  219. ret->type = XML_SCHEMA_FACET_MINLENGTH;
  220. ret->val = xmlSchemaNewValue(XML_SCHEMAS_NNINTEGER);
  221. if (ret->val == NULL) {
  222. xmlFree(ret);
  223. return(NULL);
  224. }
  225. ret->val->value.decimal.lo = value;
  226. return (ret);
  227. }
  228. /*
  229. * xmlSchemaInitBasicType:
  230. * @name: the type name
  231. * @type: the value type associated
  232. *
  233. * Initialize one primitive built-in type
  234. */
  235. static xmlSchemaTypePtr
  236. xmlSchemaInitBasicType(const char *name, xmlSchemaValType type,
  237. xmlSchemaTypePtr baseType) {
  238. xmlSchemaTypePtr ret;
  239. ret = (xmlSchemaTypePtr) xmlMalloc(sizeof(xmlSchemaType));
  240. if (ret == NULL) {
  241. xmlSchemaTypeErrMemory(NULL, "could not initialize basic types");
  242. return(NULL);
  243. }
  244. memset(ret, 0, sizeof(xmlSchemaType));
  245. ret->name = (const xmlChar *)name;
  246. ret->targetNamespace = XML_SCHEMAS_NAMESPACE_NAME;
  247. ret->type = XML_SCHEMA_TYPE_BASIC;
  248. ret->baseType = baseType;
  249. ret->contentType = XML_SCHEMA_CONTENT_BASIC;
  250. /*
  251. * Primitive types.
  252. */
  253. switch (type) {
  254. case XML_SCHEMAS_STRING:
  255. case XML_SCHEMAS_DECIMAL:
  256. case XML_SCHEMAS_DATE:
  257. case XML_SCHEMAS_DATETIME:
  258. case XML_SCHEMAS_TIME:
  259. case XML_SCHEMAS_GYEAR:
  260. case XML_SCHEMAS_GYEARMONTH:
  261. case XML_SCHEMAS_GMONTH:
  262. case XML_SCHEMAS_GMONTHDAY:
  263. case XML_SCHEMAS_GDAY:
  264. case XML_SCHEMAS_DURATION:
  265. case XML_SCHEMAS_FLOAT:
  266. case XML_SCHEMAS_DOUBLE:
  267. case XML_SCHEMAS_BOOLEAN:
  268. case XML_SCHEMAS_ANYURI:
  269. case XML_SCHEMAS_HEXBINARY:
  270. case XML_SCHEMAS_BASE64BINARY:
  271. case XML_SCHEMAS_QNAME:
  272. case XML_SCHEMAS_NOTATION:
  273. ret->flags |= XML_SCHEMAS_TYPE_BUILTIN_PRIMITIVE;
  274. break;
  275. default:
  276. break;
  277. }
  278. /*
  279. * Set variety.
  280. */
  281. switch (type) {
  282. case XML_SCHEMAS_ANYTYPE:
  283. case XML_SCHEMAS_ANYSIMPLETYPE:
  284. break;
  285. case XML_SCHEMAS_IDREFS:
  286. case XML_SCHEMAS_NMTOKENS:
  287. case XML_SCHEMAS_ENTITIES:
  288. ret->flags |= XML_SCHEMAS_TYPE_VARIETY_LIST;
  289. ret->facets = xmlSchemaNewMinLengthFacet(1);
  290. ret->flags |= XML_SCHEMAS_TYPE_HAS_FACETS;
  291. break;
  292. default:
  293. ret->flags |= XML_SCHEMAS_TYPE_VARIETY_ATOMIC;
  294. break;
  295. }
  296. xmlHashAddEntry2(xmlSchemaTypesBank, ret->name,
  297. XML_SCHEMAS_NAMESPACE_NAME, ret);
  298. ret->builtInType = type;
  299. return(ret);
  300. }
  301. /*
  302. * WARNING: Those type reside normally in xmlschemas.c but are
  303. * redefined here locally in oder of being able to use them for xs:anyType-
  304. * TODO: Remove those definition if we move the types to a header file.
  305. * TODO: Always keep those structs up-to-date with the originals.
  306. */
  307. #define UNBOUNDED (1 << 30)
  308. typedef struct _xmlSchemaTreeItem xmlSchemaTreeItem;
  309. typedef xmlSchemaTreeItem *xmlSchemaTreeItemPtr;
  310. struct _xmlSchemaTreeItem {
  311. xmlSchemaTypeType type;
  312. xmlSchemaAnnotPtr annot;
  313. xmlSchemaTreeItemPtr next;
  314. xmlSchemaTreeItemPtr children;
  315. };
  316. typedef struct _xmlSchemaParticle xmlSchemaParticle;
  317. typedef xmlSchemaParticle *xmlSchemaParticlePtr;
  318. struct _xmlSchemaParticle {
  319. xmlSchemaTypeType type;
  320. xmlSchemaAnnotPtr annot;
  321. xmlSchemaTreeItemPtr next;
  322. xmlSchemaTreeItemPtr children;
  323. int minOccurs;
  324. int maxOccurs;
  325. xmlNodePtr node;
  326. };
  327. typedef struct _xmlSchemaModelGroup xmlSchemaModelGroup;
  328. typedef xmlSchemaModelGroup *xmlSchemaModelGroupPtr;
  329. struct _xmlSchemaModelGroup {
  330. xmlSchemaTypeType type;
  331. xmlSchemaAnnotPtr annot;
  332. xmlSchemaTreeItemPtr next;
  333. xmlSchemaTreeItemPtr children;
  334. xmlNodePtr node;
  335. };
  336. static xmlSchemaParticlePtr
  337. xmlSchemaAddParticle(void)
  338. {
  339. xmlSchemaParticlePtr ret = NULL;
  340. ret = (xmlSchemaParticlePtr)
  341. xmlMalloc(sizeof(xmlSchemaParticle));
  342. if (ret == NULL) {
  343. xmlSchemaTypeErrMemory(NULL, "allocating particle component");
  344. return (NULL);
  345. }
  346. memset(ret, 0, sizeof(xmlSchemaParticle));
  347. ret->type = XML_SCHEMA_TYPE_PARTICLE;
  348. ret->minOccurs = 1;
  349. ret->maxOccurs = 1;
  350. return (ret);
  351. }
  352. /*
  353. * xmlSchemaInitTypes:
  354. *
  355. * Initialize the default XML Schemas type library
  356. */
  357. void
  358. xmlSchemaInitTypes(void)
  359. {
  360. if (xmlSchemaTypesInitialized != 0)
  361. return;
  362. xmlSchemaTypesBank = xmlHashCreate(40);
  363. /*
  364. * 3.4.7 Built-in Complex Type Definition
  365. */
  366. xmlSchemaTypeAnyTypeDef = xmlSchemaInitBasicType("anyType",
  367. XML_SCHEMAS_ANYTYPE,
  368. NULL);
  369. xmlSchemaTypeAnyTypeDef->baseType = xmlSchemaTypeAnyTypeDef;
  370. xmlSchemaTypeAnyTypeDef->contentType = XML_SCHEMA_CONTENT_MIXED;
  371. /*
  372. * Init the content type.
  373. */
  374. xmlSchemaTypeAnyTypeDef->contentType = XML_SCHEMA_CONTENT_MIXED;
  375. {
  376. xmlSchemaParticlePtr particle;
  377. xmlSchemaModelGroupPtr sequence;
  378. xmlSchemaWildcardPtr wild;
  379. /* First particle. */
  380. particle = xmlSchemaAddParticle();
  381. if (particle == NULL)
  382. return;
  383. xmlSchemaTypeAnyTypeDef->subtypes = (xmlSchemaTypePtr) particle;
  384. /* Sequence model group. */
  385. sequence = (xmlSchemaModelGroupPtr)
  386. xmlMalloc(sizeof(xmlSchemaModelGroup));
  387. if (sequence == NULL) {
  388. xmlSchemaTypeErrMemory(NULL, "allocating model group component");
  389. return;
  390. }
  391. memset(sequence, 0, sizeof(xmlSchemaModelGroup));
  392. sequence->type = XML_SCHEMA_TYPE_SEQUENCE;
  393. particle->children = (xmlSchemaTreeItemPtr) sequence;
  394. /* Second particle. */
  395. particle = xmlSchemaAddParticle();
  396. if (particle == NULL)
  397. return;
  398. particle->minOccurs = 0;
  399. particle->maxOccurs = UNBOUNDED;
  400. sequence->children = (xmlSchemaTreeItemPtr) particle;
  401. /* The wildcard */
  402. wild = (xmlSchemaWildcardPtr) xmlMalloc(sizeof(xmlSchemaWildcard));
  403. if (wild == NULL) {
  404. xmlSchemaTypeErrMemory(NULL, "allocating wildcard component");
  405. return;
  406. }
  407. memset(wild, 0, sizeof(xmlSchemaWildcard));
  408. wild->type = XML_SCHEMA_TYPE_ANY;
  409. wild->any = 1;
  410. wild->processContents = XML_SCHEMAS_ANY_LAX;
  411. particle->children = (xmlSchemaTreeItemPtr) wild;
  412. /*
  413. * Create the attribute wildcard.
  414. */
  415. wild = (xmlSchemaWildcardPtr) xmlMalloc(sizeof(xmlSchemaWildcard));
  416. if (wild == NULL) {
  417. xmlSchemaTypeErrMemory(NULL, "could not create an attribute "
  418. "wildcard on anyType");
  419. return;
  420. }
  421. memset(wild, 0, sizeof(xmlSchemaWildcard));
  422. wild->any = 1;
  423. wild->processContents = XML_SCHEMAS_ANY_LAX;
  424. xmlSchemaTypeAnyTypeDef->attributeWildcard = wild;
  425. }
  426. xmlSchemaTypeAnySimpleTypeDef = xmlSchemaInitBasicType("anySimpleType",
  427. XML_SCHEMAS_ANYSIMPLETYPE,
  428. xmlSchemaTypeAnyTypeDef);
  429. /*
  430. * primitive datatypes
  431. */
  432. xmlSchemaTypeStringDef = xmlSchemaInitBasicType("string",
  433. XML_SCHEMAS_STRING,
  434. xmlSchemaTypeAnySimpleTypeDef);
  435. xmlSchemaTypeDecimalDef = xmlSchemaInitBasicType("decimal",
  436. XML_SCHEMAS_DECIMAL,
  437. xmlSchemaTypeAnySimpleTypeDef);
  438. xmlSchemaTypeDateDef = xmlSchemaInitBasicType("date",
  439. XML_SCHEMAS_DATE,
  440. xmlSchemaTypeAnySimpleTypeDef);
  441. xmlSchemaTypeDatetimeDef = xmlSchemaInitBasicType("dateTime",
  442. XML_SCHEMAS_DATETIME,
  443. xmlSchemaTypeAnySimpleTypeDef);
  444. xmlSchemaTypeTimeDef = xmlSchemaInitBasicType("time",
  445. XML_SCHEMAS_TIME,
  446. xmlSchemaTypeAnySimpleTypeDef);
  447. xmlSchemaTypeGYearDef = xmlSchemaInitBasicType("gYear",
  448. XML_SCHEMAS_GYEAR,
  449. xmlSchemaTypeAnySimpleTypeDef);
  450. xmlSchemaTypeGYearMonthDef = xmlSchemaInitBasicType("gYearMonth",
  451. XML_SCHEMAS_GYEARMONTH,
  452. xmlSchemaTypeAnySimpleTypeDef);
  453. xmlSchemaTypeGMonthDef = xmlSchemaInitBasicType("gMonth",
  454. XML_SCHEMAS_GMONTH,
  455. xmlSchemaTypeAnySimpleTypeDef);
  456. xmlSchemaTypeGMonthDayDef = xmlSchemaInitBasicType("gMonthDay",
  457. XML_SCHEMAS_GMONTHDAY,
  458. xmlSchemaTypeAnySimpleTypeDef);
  459. xmlSchemaTypeGDayDef = xmlSchemaInitBasicType("gDay",
  460. XML_SCHEMAS_GDAY,
  461. xmlSchemaTypeAnySimpleTypeDef);
  462. xmlSchemaTypeDurationDef = xmlSchemaInitBasicType("duration",
  463. XML_SCHEMAS_DURATION,
  464. xmlSchemaTypeAnySimpleTypeDef);
  465. xmlSchemaTypeFloatDef = xmlSchemaInitBasicType("float",
  466. XML_SCHEMAS_FLOAT,
  467. xmlSchemaTypeAnySimpleTypeDef);
  468. xmlSchemaTypeDoubleDef = xmlSchemaInitBasicType("double",
  469. XML_SCHEMAS_DOUBLE,
  470. xmlSchemaTypeAnySimpleTypeDef);
  471. xmlSchemaTypeBooleanDef = xmlSchemaInitBasicType("boolean",
  472. XML_SCHEMAS_BOOLEAN,
  473. xmlSchemaTypeAnySimpleTypeDef);
  474. xmlSchemaTypeAnyURIDef = xmlSchemaInitBasicType("anyURI",
  475. XML_SCHEMAS_ANYURI,
  476. xmlSchemaTypeAnySimpleTypeDef);
  477. xmlSchemaTypeHexBinaryDef = xmlSchemaInitBasicType("hexBinary",
  478. XML_SCHEMAS_HEXBINARY,
  479. xmlSchemaTypeAnySimpleTypeDef);
  480. xmlSchemaTypeBase64BinaryDef
  481. = xmlSchemaInitBasicType("base64Binary", XML_SCHEMAS_BASE64BINARY,
  482. xmlSchemaTypeAnySimpleTypeDef);
  483. xmlSchemaTypeNotationDef = xmlSchemaInitBasicType("NOTATION",
  484. XML_SCHEMAS_NOTATION,
  485. xmlSchemaTypeAnySimpleTypeDef);
  486. xmlSchemaTypeQNameDef = xmlSchemaInitBasicType("QName",
  487. XML_SCHEMAS_QNAME,
  488. xmlSchemaTypeAnySimpleTypeDef);
  489. /*
  490. * derived datatypes
  491. */
  492. xmlSchemaTypeIntegerDef = xmlSchemaInitBasicType("integer",
  493. XML_SCHEMAS_INTEGER,
  494. xmlSchemaTypeDecimalDef);
  495. xmlSchemaTypeNonPositiveIntegerDef =
  496. xmlSchemaInitBasicType("nonPositiveInteger",
  497. XML_SCHEMAS_NPINTEGER,
  498. xmlSchemaTypeIntegerDef);
  499. xmlSchemaTypeNegativeIntegerDef =
  500. xmlSchemaInitBasicType("negativeInteger", XML_SCHEMAS_NINTEGER,
  501. xmlSchemaTypeNonPositiveIntegerDef);
  502. xmlSchemaTypeLongDef =
  503. xmlSchemaInitBasicType("long", XML_SCHEMAS_LONG,
  504. xmlSchemaTypeIntegerDef);
  505. xmlSchemaTypeIntDef = xmlSchemaInitBasicType("int", XML_SCHEMAS_INT,
  506. xmlSchemaTypeLongDef);
  507. xmlSchemaTypeShortDef = xmlSchemaInitBasicType("short",
  508. XML_SCHEMAS_SHORT,
  509. xmlSchemaTypeIntDef);
  510. xmlSchemaTypeByteDef = xmlSchemaInitBasicType("byte",
  511. XML_SCHEMAS_BYTE,
  512. xmlSchemaTypeShortDef);
  513. xmlSchemaTypeNonNegativeIntegerDef =
  514. xmlSchemaInitBasicType("nonNegativeInteger",
  515. XML_SCHEMAS_NNINTEGER,
  516. xmlSchemaTypeIntegerDef);
  517. xmlSchemaTypeUnsignedLongDef =
  518. xmlSchemaInitBasicType("unsignedLong", XML_SCHEMAS_ULONG,
  519. xmlSchemaTypeNonNegativeIntegerDef);
  520. xmlSchemaTypeUnsignedIntDef =
  521. xmlSchemaInitBasicType("unsignedInt", XML_SCHEMAS_UINT,
  522. xmlSchemaTypeUnsignedLongDef);
  523. xmlSchemaTypeUnsignedShortDef =
  524. xmlSchemaInitBasicType("unsignedShort", XML_SCHEMAS_USHORT,
  525. xmlSchemaTypeUnsignedIntDef);
  526. xmlSchemaTypeUnsignedByteDef =
  527. xmlSchemaInitBasicType("unsignedByte", XML_SCHEMAS_UBYTE,
  528. xmlSchemaTypeUnsignedShortDef);
  529. xmlSchemaTypePositiveIntegerDef =
  530. xmlSchemaInitBasicType("positiveInteger", XML_SCHEMAS_PINTEGER,
  531. xmlSchemaTypeNonNegativeIntegerDef);
  532. xmlSchemaTypeNormStringDef = xmlSchemaInitBasicType("normalizedString",
  533. XML_SCHEMAS_NORMSTRING,
  534. xmlSchemaTypeStringDef);
  535. xmlSchemaTypeTokenDef = xmlSchemaInitBasicType("token",
  536. XML_SCHEMAS_TOKEN,
  537. xmlSchemaTypeNormStringDef);
  538. xmlSchemaTypeLanguageDef = xmlSchemaInitBasicType("language",
  539. XML_SCHEMAS_LANGUAGE,
  540. xmlSchemaTypeTokenDef);
  541. xmlSchemaTypeNameDef = xmlSchemaInitBasicType("Name",
  542. XML_SCHEMAS_NAME,
  543. xmlSchemaTypeTokenDef);
  544. xmlSchemaTypeNmtokenDef = xmlSchemaInitBasicType("NMTOKEN",
  545. XML_SCHEMAS_NMTOKEN,
  546. xmlSchemaTypeTokenDef);
  547. xmlSchemaTypeNCNameDef = xmlSchemaInitBasicType("NCName",
  548. XML_SCHEMAS_NCNAME,
  549. xmlSchemaTypeNameDef);
  550. xmlSchemaTypeIdDef = xmlSchemaInitBasicType("ID", XML_SCHEMAS_ID,
  551. xmlSchemaTypeNCNameDef);
  552. xmlSchemaTypeIdrefDef = xmlSchemaInitBasicType("IDREF",
  553. XML_SCHEMAS_IDREF,
  554. xmlSchemaTypeNCNameDef);
  555. xmlSchemaTypeEntityDef = xmlSchemaInitBasicType("ENTITY",
  556. XML_SCHEMAS_ENTITY,
  557. xmlSchemaTypeNCNameDef);
  558. /*
  559. * Derived list types.
  560. */
  561. /* ENTITIES */
  562. xmlSchemaTypeEntitiesDef = xmlSchemaInitBasicType("ENTITIES",
  563. XML_SCHEMAS_ENTITIES,
  564. xmlSchemaTypeAnySimpleTypeDef);
  565. xmlSchemaTypeEntitiesDef->subtypes = xmlSchemaTypeEntityDef;
  566. /* IDREFS */
  567. xmlSchemaTypeIdrefsDef = xmlSchemaInitBasicType("IDREFS",
  568. XML_SCHEMAS_IDREFS,
  569. xmlSchemaTypeAnySimpleTypeDef);
  570. xmlSchemaTypeIdrefsDef->subtypes = xmlSchemaTypeIdrefDef;
  571. /* NMTOKENS */
  572. xmlSchemaTypeNmtokensDef = xmlSchemaInitBasicType("NMTOKENS",
  573. XML_SCHEMAS_NMTOKENS,
  574. xmlSchemaTypeAnySimpleTypeDef);
  575. xmlSchemaTypeNmtokensDef->subtypes = xmlSchemaTypeNmtokenDef;
  576. xmlSchemaTypesInitialized = 1;
  577. }
  578. static void
  579. xmlSchemaFreeTypeEntry(void *type, const xmlChar *name ATTRIBUTE_UNUSED) {
  580. xmlSchemaFreeType((xmlSchemaTypePtr) type);
  581. }
  582. /**
  583. * xmlSchemaCleanupTypes:
  584. *
  585. * Cleanup the default XML Schemas type library
  586. */
  587. void
  588. xmlSchemaCleanupTypes(void) {
  589. if (xmlSchemaTypesInitialized == 0)
  590. return;
  591. /*
  592. * Free xs:anyType.
  593. */
  594. {
  595. xmlSchemaParticlePtr particle;
  596. /* Attribute wildcard. */
  597. xmlSchemaFreeWildcard(xmlSchemaTypeAnyTypeDef->attributeWildcard);
  598. /* Content type. */
  599. particle = (xmlSchemaParticlePtr) xmlSchemaTypeAnyTypeDef->subtypes;
  600. /* Wildcard. */
  601. xmlSchemaFreeWildcard((xmlSchemaWildcardPtr)
  602. particle->children->children->children);
  603. xmlFree((xmlSchemaParticlePtr) particle->children->children);
  604. /* Sequence model group. */
  605. xmlFree((xmlSchemaModelGroupPtr) particle->children);
  606. xmlFree((xmlSchemaParticlePtr) particle);
  607. xmlSchemaTypeAnyTypeDef->subtypes = NULL;
  608. }
  609. xmlHashFree(xmlSchemaTypesBank, xmlSchemaFreeTypeEntry);
  610. xmlSchemaTypesInitialized = 0;
  611. }
  612. /**
  613. * xmlSchemaIsBuiltInTypeFacet:
  614. * @type: the built-in type
  615. * @facetType: the facet type
  616. *
  617. * Evaluates if a specific facet can be
  618. * used in conjunction with a type.
  619. *
  620. * Returns 1 if the facet can be used with the given built-in type,
  621. * 0 otherwise and -1 in case the type is not a built-in type.
  622. */
  623. int
  624. xmlSchemaIsBuiltInTypeFacet(xmlSchemaTypePtr type, int facetType)
  625. {
  626. if (type == NULL)
  627. return (-1);
  628. if (type->type != XML_SCHEMA_TYPE_BASIC)
  629. return (-1);
  630. switch (type->builtInType) {
  631. case XML_SCHEMAS_BOOLEAN:
  632. if ((facetType == XML_SCHEMA_FACET_PATTERN) ||
  633. (facetType == XML_SCHEMA_FACET_WHITESPACE))
  634. return (1);
  635. else
  636. return (0);
  637. case XML_SCHEMAS_STRING:
  638. case XML_SCHEMAS_NOTATION:
  639. case XML_SCHEMAS_QNAME:
  640. case XML_SCHEMAS_ANYURI:
  641. case XML_SCHEMAS_BASE64BINARY:
  642. case XML_SCHEMAS_HEXBINARY:
  643. if ((facetType == XML_SCHEMA_FACET_LENGTH) ||
  644. (facetType == XML_SCHEMA_FACET_MINLENGTH) ||
  645. (facetType == XML_SCHEMA_FACET_MAXLENGTH) ||
  646. (facetType == XML_SCHEMA_FACET_PATTERN) ||
  647. (facetType == XML_SCHEMA_FACET_ENUMERATION) ||
  648. (facetType == XML_SCHEMA_FACET_WHITESPACE))
  649. return (1);
  650. else
  651. return (0);
  652. case XML_SCHEMAS_DECIMAL:
  653. if ((facetType == XML_SCHEMA_FACET_TOTALDIGITS) ||
  654. (facetType == XML_SCHEMA_FACET_FRACTIONDIGITS) ||
  655. (facetType == XML_SCHEMA_FACET_PATTERN) ||
  656. (facetType == XML_SCHEMA_FACET_WHITESPACE) ||
  657. (facetType == XML_SCHEMA_FACET_ENUMERATION) ||
  658. (facetType == XML_SCHEMA_FACET_MAXINCLUSIVE) ||
  659. (facetType == XML_SCHEMA_FACET_MAXEXCLUSIVE) ||
  660. (facetType == XML_SCHEMA_FACET_MININCLUSIVE) ||
  661. (facetType == XML_SCHEMA_FACET_MINEXCLUSIVE))
  662. return (1);
  663. else
  664. return (0);
  665. case XML_SCHEMAS_TIME:
  666. case XML_SCHEMAS_GDAY:
  667. case XML_SCHEMAS_GMONTH:
  668. case XML_SCHEMAS_GMONTHDAY:
  669. case XML_SCHEMAS_GYEAR:
  670. case XML_SCHEMAS_GYEARMONTH:
  671. case XML_SCHEMAS_DATE:
  672. case XML_SCHEMAS_DATETIME:
  673. case XML_SCHEMAS_DURATION:
  674. case XML_SCHEMAS_FLOAT:
  675. case XML_SCHEMAS_DOUBLE:
  676. if ((facetType == XML_SCHEMA_FACET_PATTERN) ||
  677. (facetType == XML_SCHEMA_FACET_ENUMERATION) ||
  678. (facetType == XML_SCHEMA_FACET_WHITESPACE) ||
  679. (facetType == XML_SCHEMA_FACET_MAXINCLUSIVE) ||
  680. (facetType == XML_SCHEMA_FACET_MAXEXCLUSIVE) ||
  681. (facetType == XML_SCHEMA_FACET_MININCLUSIVE) ||
  682. (facetType == XML_SCHEMA_FACET_MINEXCLUSIVE))
  683. return (1);
  684. else
  685. return (0);
  686. default:
  687. break;
  688. }
  689. return (0);
  690. }
  691. /**
  692. * xmlSchemaGetBuiltInType:
  693. * @type: the type of the built in type
  694. *
  695. * Gives you the type struct for a built-in
  696. * type by its type id.
  697. *
  698. * Returns the type if found, NULL otherwise.
  699. */
  700. xmlSchemaTypePtr
  701. xmlSchemaGetBuiltInType(xmlSchemaValType type)
  702. {
  703. if (xmlSchemaTypesInitialized == 0)
  704. xmlSchemaInitTypes();
  705. switch (type) {
  706. case XML_SCHEMAS_ANYSIMPLETYPE:
  707. return (xmlSchemaTypeAnySimpleTypeDef);
  708. case XML_SCHEMAS_STRING:
  709. return (xmlSchemaTypeStringDef);
  710. case XML_SCHEMAS_NORMSTRING:
  711. return (xmlSchemaTypeNormStringDef);
  712. case XML_SCHEMAS_DECIMAL:
  713. return (xmlSchemaTypeDecimalDef);
  714. case XML_SCHEMAS_TIME:
  715. return (xmlSchemaTypeTimeDef);
  716. case XML_SCHEMAS_GDAY:
  717. return (xmlSchemaTypeGDayDef);
  718. case XML_SCHEMAS_GMONTH:
  719. return (xmlSchemaTypeGMonthDef);
  720. case XML_SCHEMAS_GMONTHDAY:
  721. return (xmlSchemaTypeGMonthDayDef);
  722. case XML_SCHEMAS_GYEAR:
  723. return (xmlSchemaTypeGYearDef);
  724. case XML_SCHEMAS_GYEARMONTH:
  725. return (xmlSchemaTypeGYearMonthDef);
  726. case XML_SCHEMAS_DATE:
  727. return (xmlSchemaTypeDateDef);
  728. case XML_SCHEMAS_DATETIME:
  729. return (xmlSchemaTypeDatetimeDef);
  730. case XML_SCHEMAS_DURATION:
  731. return (xmlSchemaTypeDurationDef);
  732. case XML_SCHEMAS_FLOAT:
  733. return (xmlSchemaTypeFloatDef);
  734. case XML_SCHEMAS_DOUBLE:
  735. return (xmlSchemaTypeDoubleDef);
  736. case XML_SCHEMAS_BOOLEAN:
  737. return (xmlSchemaTypeBooleanDef);
  738. case XML_SCHEMAS_TOKEN:
  739. return (xmlSchemaTypeTokenDef);
  740. case XML_SCHEMAS_LANGUAGE:
  741. return (xmlSchemaTypeLanguageDef);
  742. case XML_SCHEMAS_NMTOKEN:
  743. return (xmlSchemaTypeNmtokenDef);
  744. case XML_SCHEMAS_NMTOKENS:
  745. return (xmlSchemaTypeNmtokensDef);
  746. case XML_SCHEMAS_NAME:
  747. return (xmlSchemaTypeNameDef);
  748. case XML_SCHEMAS_QNAME:
  749. return (xmlSchemaTypeQNameDef);
  750. case XML_SCHEMAS_NCNAME:
  751. return (xmlSchemaTypeNCNameDef);
  752. case XML_SCHEMAS_ID:
  753. return (xmlSchemaTypeIdDef);
  754. case XML_SCHEMAS_IDREF:
  755. return (xmlSchemaTypeIdrefDef);
  756. case XML_SCHEMAS_IDREFS:
  757. return (xmlSchemaTypeIdrefsDef);
  758. case XML_SCHEMAS_ENTITY:
  759. return (xmlSchemaTypeEntityDef);
  760. case XML_SCHEMAS_ENTITIES:
  761. return (xmlSchemaTypeEntitiesDef);
  762. case XML_SCHEMAS_NOTATION:
  763. return (xmlSchemaTypeNotationDef);
  764. case XML_SCHEMAS_ANYURI:
  765. return (xmlSchemaTypeAnyURIDef);
  766. case XML_SCHEMAS_INTEGER:
  767. return (xmlSchemaTypeIntegerDef);
  768. case XML_SCHEMAS_NPINTEGER:
  769. return (xmlSchemaTypeNonPositiveIntegerDef);
  770. case XML_SCHEMAS_NINTEGER:
  771. return (xmlSchemaTypeNegativeIntegerDef);
  772. case XML_SCHEMAS_NNINTEGER:
  773. return (xmlSchemaTypeNonNegativeIntegerDef);
  774. case XML_SCHEMAS_PINTEGER:
  775. return (xmlSchemaTypePositiveIntegerDef);
  776. case XML_SCHEMAS_INT:
  777. return (xmlSchemaTypeIntDef);
  778. case XML_SCHEMAS_UINT:
  779. return (xmlSchemaTypeUnsignedIntDef);
  780. case XML_SCHEMAS_LONG:
  781. return (xmlSchemaTypeLongDef);
  782. case XML_SCHEMAS_ULONG:
  783. return (xmlSchemaTypeUnsignedLongDef);
  784. case XML_SCHEMAS_SHORT:
  785. return (xmlSchemaTypeShortDef);
  786. case XML_SCHEMAS_USHORT:
  787. return (xmlSchemaTypeUnsignedShortDef);
  788. case XML_SCHEMAS_BYTE:
  789. return (xmlSchemaTypeByteDef);
  790. case XML_SCHEMAS_UBYTE:
  791. return (xmlSchemaTypeUnsignedByteDef);
  792. case XML_SCHEMAS_HEXBINARY:
  793. return (xmlSchemaTypeHexBinaryDef);
  794. case XML_SCHEMAS_BASE64BINARY:
  795. return (xmlSchemaTypeBase64BinaryDef);
  796. case XML_SCHEMAS_ANYTYPE:
  797. return (xmlSchemaTypeAnyTypeDef);
  798. default:
  799. return (NULL);
  800. }
  801. }
  802. /**
  803. * xmlSchemaValueAppend:
  804. * @prev: the value
  805. * @cur: the value to be appended
  806. *
  807. * Appends a next sibling to a list of computed values.
  808. *
  809. * Returns 0 if succeeded and -1 on API errors.
  810. */
  811. int
  812. xmlSchemaValueAppend(xmlSchemaValPtr prev, xmlSchemaValPtr cur) {
  813. if ((prev == NULL) || (cur == NULL))
  814. return (-1);
  815. prev->next = cur;
  816. return (0);
  817. }
  818. /**
  819. * xmlSchemaValueGetNext:
  820. * @cur: the value
  821. *
  822. * Accessor for the next sibling of a list of computed values.
  823. *
  824. * Returns the next value or NULL if there was none, or on
  825. * API errors.
  826. */
  827. xmlSchemaValPtr
  828. xmlSchemaValueGetNext(xmlSchemaValPtr cur) {
  829. if (cur == NULL)
  830. return (NULL);
  831. return (cur->next);
  832. }
  833. /**
  834. * xmlSchemaValueGetAsString:
  835. * @val: the value
  836. *
  837. * Accessor for the string value of a computed value.
  838. *
  839. * Returns the string value or NULL if there was none, or on
  840. * API errors.
  841. */
  842. const xmlChar *
  843. xmlSchemaValueGetAsString(xmlSchemaValPtr val)
  844. {
  845. if (val == NULL)
  846. return (NULL);
  847. switch (val->type) {
  848. case XML_SCHEMAS_STRING:
  849. case XML_SCHEMAS_NORMSTRING:
  850. case XML_SCHEMAS_ANYSIMPLETYPE:
  851. case XML_SCHEMAS_TOKEN:
  852. case XML_SCHEMAS_LANGUAGE:
  853. case XML_SCHEMAS_NMTOKEN:
  854. case XML_SCHEMAS_NAME:
  855. case XML_SCHEMAS_NCNAME:
  856. case XML_SCHEMAS_ID:
  857. case XML_SCHEMAS_IDREF:
  858. case XML_SCHEMAS_ENTITY:
  859. case XML_SCHEMAS_ANYURI:
  860. return (BAD_CAST val->value.str);
  861. default:
  862. break;
  863. }
  864. return (NULL);
  865. }
  866. /**
  867. * xmlSchemaValueGetAsBoolean:
  868. * @val: the value
  869. *
  870. * Accessor for the boolean value of a computed value.
  871. *
  872. * Returns 1 if true and 0 if false, or in case of an error. Hmm.
  873. */
  874. int
  875. xmlSchemaValueGetAsBoolean(xmlSchemaValPtr val)
  876. {
  877. if ((val == NULL) || (val->type != XML_SCHEMAS_BOOLEAN))
  878. return (0);
  879. return (val->value.b);
  880. }
  881. /**
  882. * xmlSchemaNewStringValue:
  883. * @type: the value type
  884. * @value: the value
  885. *
  886. * Allocate a new simple type value. The type can be
  887. * of XML_SCHEMAS_STRING.
  888. * WARNING: This one is intended to be expanded for other
  889. * string based types. We need this for anySimpleType as well.
  890. * The given value is consumed and freed with the struct.
  891. *
  892. * Returns a pointer to the new value or NULL in case of error
  893. */
  894. xmlSchemaValPtr
  895. xmlSchemaNewStringValue(xmlSchemaValType type,
  896. const xmlChar *value)
  897. {
  898. xmlSchemaValPtr val;
  899. if (type != XML_SCHEMAS_STRING)
  900. return(NULL);
  901. val = (xmlSchemaValPtr) xmlMalloc(sizeof(xmlSchemaVal));
  902. if (val == NULL) {
  903. return(NULL);
  904. }
  905. memset(val, 0, sizeof(xmlSchemaVal));
  906. val->type = type;
  907. val->value.str = (xmlChar *) value;
  908. return(val);
  909. }
  910. /**
  911. * xmlSchemaNewNOTATIONValue:
  912. * @name: the notation name
  913. * @ns: the notation namespace name or NULL
  914. *
  915. * Allocate a new NOTATION value.
  916. * The given values are consumed and freed with the struct.
  917. *
  918. * Returns a pointer to the new value or NULL in case of error
  919. */
  920. xmlSchemaValPtr
  921. xmlSchemaNewNOTATIONValue(const xmlChar *name,
  922. const xmlChar *ns)
  923. {
  924. xmlSchemaValPtr val;
  925. val = xmlSchemaNewValue(XML_SCHEMAS_NOTATION);
  926. if (val == NULL)
  927. return (NULL);
  928. val->value.qname.name = (xmlChar *)name;
  929. if (ns != NULL)
  930. val->value.qname.uri = (xmlChar *)ns;
  931. return(val);
  932. }
  933. /**
  934. * xmlSchemaNewQNameValue:
  935. * @namespaceName: the namespace name
  936. * @localName: the local name
  937. *
  938. * Allocate a new QName value.
  939. * The given values are consumed and freed with the struct.
  940. *
  941. * Returns a pointer to the new value or NULL in case of an error.
  942. */
  943. xmlSchemaValPtr
  944. xmlSchemaNewQNameValue(const xmlChar *namespaceName,
  945. const xmlChar *localName)
  946. {
  947. xmlSchemaValPtr val;
  948. val = xmlSchemaNewValue(XML_SCHEMAS_QNAME);
  949. if (val == NULL)
  950. return (NULL);
  951. val->value.qname.name = (xmlChar *) localName;
  952. val->value.qname.uri = (xmlChar *) namespaceName;
  953. return(val);
  954. }
  955. /**
  956. * xmlSchemaFreeValue:
  957. * @value: the value to free
  958. *
  959. * Cleanup the default XML Schemas type library
  960. */
  961. void
  962. xmlSchemaFreeValue(xmlSchemaValPtr value) {
  963. xmlSchemaValPtr prev;
  964. while (value != NULL) {
  965. switch (value->type) {
  966. case XML_SCHEMAS_STRING:
  967. case XML_SCHEMAS_NORMSTRING:
  968. case XML_SCHEMAS_TOKEN:
  969. case XML_SCHEMAS_LANGUAGE:
  970. case XML_SCHEMAS_NMTOKEN:
  971. case XML_SCHEMAS_NMTOKENS:
  972. case XML_SCHEMAS_NAME:
  973. case XML_SCHEMAS_NCNAME:
  974. case XML_SCHEMAS_ID:
  975. case XML_SCHEMAS_IDREF:
  976. case XML_SCHEMAS_IDREFS:
  977. case XML_SCHEMAS_ENTITY:
  978. case XML_SCHEMAS_ENTITIES:
  979. case XML_SCHEMAS_ANYURI:
  980. case XML_SCHEMAS_ANYSIMPLETYPE:
  981. if (value->value.str != NULL)
  982. xmlFree(value->value.str);
  983. break;
  984. case XML_SCHEMAS_NOTATION:
  985. case XML_SCHEMAS_QNAME:
  986. if (value->value.qname.uri != NULL)
  987. xmlFree(value->value.qname.uri);
  988. if (value->value.qname.name != NULL)
  989. xmlFree(value->value.qname.name);
  990. break;
  991. case XML_SCHEMAS_HEXBINARY:
  992. if (value->value.hex.str != NULL)
  993. xmlFree(value->value.hex.str);
  994. break;
  995. case XML_SCHEMAS_BASE64BINARY:
  996. if (value->value.base64.str != NULL)
  997. xmlFree(value->value.base64.str);
  998. break;
  999. default:
  1000. break;
  1001. }
  1002. prev = value;
  1003. value = value->next;
  1004. xmlFree(prev);
  1005. }
  1006. }
  1007. /**
  1008. * xmlSchemaGetPredefinedType:
  1009. * @name: the type name
  1010. * @ns: the URI of the namespace usually "http://www.w3.org/2001/XMLSchema"
  1011. *
  1012. * Lookup a type in the default XML Schemas type library
  1013. *
  1014. * Returns the type if found, NULL otherwise
  1015. */
  1016. xmlSchemaTypePtr
  1017. xmlSchemaGetPredefinedType(const xmlChar *name, const xmlChar *ns) {
  1018. if (xmlSchemaTypesInitialized == 0)
  1019. xmlSchemaInitTypes();
  1020. if (name == NULL)
  1021. return(NULL);
  1022. return((xmlSchemaTypePtr) xmlHashLookup2(xmlSchemaTypesBank, name, ns));
  1023. }
  1024. /**
  1025. * xmlSchemaGetBuiltInListSimpleTypeItemType:
  1026. * @type: the built-in simple type.
  1027. *
  1028. * Lookup function
  1029. *
  1030. * Returns the item type of @type as defined by the built-in datatype
  1031. * hierarchy of XML Schema Part 2: Datatypes, or NULL in case of an error.
  1032. */
  1033. xmlSchemaTypePtr
  1034. xmlSchemaGetBuiltInListSimpleTypeItemType(xmlSchemaTypePtr type)
  1035. {
  1036. if ((type == NULL) || (type->type != XML_SCHEMA_TYPE_BASIC))
  1037. return (NULL);
  1038. switch (type->builtInType) {
  1039. case XML_SCHEMAS_NMTOKENS:
  1040. return (xmlSchemaTypeNmtokenDef );
  1041. case XML_SCHEMAS_IDREFS:
  1042. return (xmlSchemaTypeIdrefDef);
  1043. case XML_SCHEMAS_ENTITIES:
  1044. return (xmlSchemaTypeEntityDef);
  1045. default:
  1046. return (NULL);
  1047. }
  1048. }
  1049. /****************************************************************
  1050. * *
  1051. * Convenience macros and functions *
  1052. * *
  1053. ****************************************************************/
  1054. #define IS_TZO_CHAR(c) \
  1055. ((c == 0) || (c == 'Z') || (c == '+') || (c == '-'))
  1056. #define VALID_YEAR(yr) (yr != 0)
  1057. #define VALID_MONTH(mon) ((mon >= 1) && (mon <= 12))
  1058. /* VALID_DAY should only be used when month is unknown */
  1059. #define VALID_DAY(day) ((day >= 1) && (day <= 31))
  1060. #define VALID_HOUR(hr) ((hr >= 0) && (hr <= 23))
  1061. #define VALID_MIN(min) ((min >= 0) && (min <= 59))
  1062. #define VALID_SEC(sec) ((sec >= 0) && (sec < 60))
  1063. #define VALID_TZO(tzo) ((tzo >= -840) && (tzo <= 840))
  1064. #define IS_LEAP(y) \
  1065. (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0))
  1066. static const unsigned int daysInMonth[12] =
  1067. { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  1068. static const unsigned int daysInMonthLeap[12] =
  1069. { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  1070. #define MAX_DAYINMONTH(yr,mon) \
  1071. (IS_LEAP(yr) ? daysInMonthLeap[mon - 1] : daysInMonth[mon - 1])
  1072. #define VALID_MDAY(dt) \
  1073. (IS_LEAP(dt->year) ? \
  1074. (dt->day <= daysInMonthLeap[dt->mon - 1]) : \
  1075. (dt->day <= daysInMonth[dt->mon - 1]))
  1076. #define VALID_DATE(dt) \
  1077. (VALID_YEAR(dt->year) && VALID_MONTH(dt->mon) && VALID_MDAY(dt))
  1078. #define VALID_END_OF_DAY(dt) \
  1079. ((dt)->hour == 24 && (dt)->min == 0 && (dt)->sec == 0)
  1080. #define VALID_TIME(dt) \
  1081. (((VALID_HOUR(dt->hour) && VALID_MIN(dt->min) && \
  1082. VALID_SEC(dt->sec)) || VALID_END_OF_DAY(dt)) && \
  1083. VALID_TZO(dt->tzo))
  1084. #define VALID_DATETIME(dt) \
  1085. (VALID_DATE(dt) && VALID_TIME(dt))
  1086. #define SECS_PER_MIN 60
  1087. #define MINS_PER_HOUR 60
  1088. #define HOURS_PER_DAY 24
  1089. #define SECS_PER_HOUR (MINS_PER_HOUR * SECS_PER_MIN)
  1090. #define SECS_PER_DAY (HOURS_PER_DAY * SECS_PER_HOUR)
  1091. #define MINS_PER_DAY (HOURS_PER_DAY * MINS_PER_HOUR)
  1092. static const long dayInYearByMonth[12] =
  1093. { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
  1094. static const long dayInLeapYearByMonth[12] =
  1095. { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 };
  1096. #define DAY_IN_YEAR(day, month, year) \
  1097. ((IS_LEAP(year) ? \
  1098. dayInLeapYearByMonth[month - 1] : \
  1099. dayInYearByMonth[month - 1]) + day)
  1100. #ifdef DEBUG
  1101. #define DEBUG_DATE(dt) \
  1102. xmlGenericError(xmlGenericErrorContext, \
  1103. "type=%o %04ld-%02u-%02uT%02u:%02u:%03f", \
  1104. dt->type,dt->value.date.year,dt->value.date.mon, \
  1105. dt->value.date.day,dt->value.date.hour,dt->value.date.min, \
  1106. dt->value.date.sec); \
  1107. if (dt->value.date.tz_flag) \
  1108. if (dt->value.date.tzo != 0) \
  1109. xmlGenericError(xmlGenericErrorContext, \
  1110. "%+05d\n",dt->value.date.tzo); \
  1111. else \
  1112. xmlGenericError(xmlGenericErrorContext, "Z\n"); \
  1113. else \
  1114. xmlGenericError(xmlGenericErrorContext,"\n")
  1115. #else
  1116. #define DEBUG_DATE(dt)
  1117. #endif
  1118. /**
  1119. * _xmlSchemaParseGYear:
  1120. * @dt: pointer to a date structure
  1121. * @str: pointer to the string to analyze
  1122. *
  1123. * Parses a xs:gYear without time zone and fills in the appropriate
  1124. * field of the @dt structure. @str is updated to point just after the
  1125. * xs:gYear. It is supposed that @dt->year is big enough to contain
  1126. * the year.
  1127. *
  1128. * Returns 0 or the error code
  1129. */
  1130. static int
  1131. _xmlSchemaParseGYear (xmlSchemaValDatePtr dt, const xmlChar **str) {
  1132. const xmlChar *cur = *str, *firstChar;
  1133. int isneg = 0, digcnt = 0;
  1134. if (((*cur < '0') || (*cur > '9')) &&
  1135. (*cur != '-') && (*cur != '+'))
  1136. return -1;
  1137. if (*cur == '-') {
  1138. isneg = 1;
  1139. cur++;
  1140. }
  1141. firstChar = cur;
  1142. while ((*cur >= '0') && (*cur <= '9')) {
  1143. int digit = *cur - '0';
  1144. if (dt->year > LONG_MAX / 10)
  1145. return 2;
  1146. dt->year *= 10;
  1147. if (dt->year > LONG_MAX - digit)
  1148. return 2;
  1149. dt->year += digit;
  1150. cur++;
  1151. digcnt++;
  1152. }
  1153. /* year must be at least 4 digits (CCYY); over 4
  1154. * digits cannot have a leading zero. */
  1155. if ((digcnt < 4) || ((digcnt > 4) && (*firstChar == '0')))
  1156. return 1;
  1157. if (isneg)
  1158. dt->year = - dt->year;
  1159. if (!VALID_YEAR(dt->year))
  1160. return 2;
  1161. *str = cur;
  1162. return 0;
  1163. }
  1164. /**
  1165. * PARSE_2_DIGITS:
  1166. * @num: the integer to fill in
  1167. * @cur: an #xmlChar *
  1168. * @invalid: an integer
  1169. *
  1170. * Parses a 2-digits integer and updates @num with the value. @cur is
  1171. * updated to point just after the integer.
  1172. * In case of error, @invalid is set to %TRUE, values of @num and
  1173. * @cur are undefined.
  1174. */
  1175. #define PARSE_2_DIGITS(num, cur, invalid) \
  1176. if ((cur[0] < '0') || (cur[0] > '9') || \
  1177. (cur[1] < '0') || (cur[1] > '9')) \
  1178. invalid = 1; \
  1179. else \
  1180. num = (cur[0] - '0') * 10 + (cur[1] - '0'); \
  1181. cur += 2;
  1182. /**
  1183. * PARSE_FLOAT:
  1184. * @num: the double to fill in
  1185. * @cur: an #xmlChar *
  1186. * @invalid: an integer
  1187. *
  1188. * Parses a float and updates @num with the value. @cur is
  1189. * updated to point just after the float. The float must have a
  1190. * 2-digits integer part and may or may not have a decimal part.
  1191. * In case of error, @invalid is set to %TRUE, values of @num and
  1192. * @cur are undefined.
  1193. */
  1194. #define PARSE_FLOAT(num, cur, invalid) \
  1195. PARSE_2_DIGITS(num, cur, invalid); \
  1196. if (!invalid && (*cur == '.')) { \
  1197. double mult = 1; \
  1198. cur++; \
  1199. if ((*cur < '0') || (*cur > '9')) \
  1200. invalid = 1; \
  1201. while ((*cur >= '0') && (*cur <= '9')) { \
  1202. mult /= 10; \
  1203. num += (*cur - '0') * mult; \
  1204. cur++; \
  1205. } \
  1206. }
  1207. /**
  1208. * _xmlSchemaParseGMonth:
  1209. * @dt: pointer to a date structure
  1210. * @str: pointer to the string to analyze
  1211. *
  1212. * Parses a xs:gMonth without time zone and fills in the appropriate
  1213. * field of the @dt structure. @str is updated to point just after the
  1214. * xs:gMonth.
  1215. *
  1216. * Returns 0 or the error code
  1217. */
  1218. static int
  1219. _xmlSchemaParseGMonth (xmlSchemaValDatePtr dt, const xmlChar **str) {
  1220. const xmlChar *cur = *str;
  1221. int ret = 0;
  1222. unsigned int value = 0;
  1223. PARSE_2_DIGITS(value, cur, ret);
  1224. if (ret != 0)
  1225. return ret;
  1226. if (!VALID_MONTH(value))
  1227. return 2;
  1228. dt->mon = value;
  1229. *str = cur;
  1230. return 0;
  1231. }
  1232. /**
  1233. * _xmlSchemaParseGDay:
  1234. * @dt: pointer to a date structure
  1235. * @str: pointer to the string to analyze
  1236. *
  1237. * Parses a xs:gDay without time zone and fills in the appropriate
  1238. * field of the @dt structure. @str is updated to point just after the
  1239. * xs:gDay.
  1240. *
  1241. * Returns 0 or the error code
  1242. */
  1243. static int
  1244. _xmlSchemaParseGDay (xmlSchemaValDatePtr dt, const xmlChar **str) {
  1245. const xmlChar *cur = *str;
  1246. int ret = 0;
  1247. unsigned int value = 0;
  1248. PARSE_2_DIGITS(value, cur, ret);
  1249. if (ret != 0)
  1250. return ret;
  1251. if (!VALID_DAY(value))
  1252. return 2;
  1253. dt->day = value;
  1254. *str = cur;
  1255. return 0;
  1256. }
  1257. /**
  1258. * _xmlSchemaParseTime:
  1259. * @dt: pointer to a date structure
  1260. * @str: pointer to the string to analyze
  1261. *
  1262. * Parses a xs:time without time zone and fills in the appropriate
  1263. * fields of the @dt structure. @str is updated to point just after the
  1264. * xs:time.
  1265. * In case of error, values of @dt fields are undefined.
  1266. *
  1267. * Returns 0 or the error code
  1268. */
  1269. static int
  1270. _xmlSchemaParseTime (xmlSchemaValDatePtr dt, const xmlChar **str) {
  1271. const xmlChar *cur = *str;
  1272. int ret = 0;
  1273. int value = 0;
  1274. PARSE_2_DIGITS(value, cur, ret);
  1275. if (ret != 0)
  1276. return ret;
  1277. if (*cur != ':')
  1278. return 1;
  1279. if (!VALID_HOUR(value) && value != 24 /* Allow end-of-day hour */)
  1280. return 2;
  1281. cur++;
  1282. /* the ':' insures this string is xs:time */
  1283. dt->hour = value;
  1284. PARSE_2_DIGITS(value, cur, ret);
  1285. if (ret != 0)
  1286. return ret;
  1287. if (!VALID_MIN(value))
  1288. return 2;
  1289. dt->min = value;
  1290. if (*cur != ':')
  1291. return 1;
  1292. cur++;
  1293. PARSE_FLOAT(dt->sec, cur, ret);
  1294. if (ret != 0)
  1295. return ret;
  1296. if (!VALID_TIME(dt))
  1297. return 2;
  1298. *str = cur;
  1299. return 0;
  1300. }
  1301. /**
  1302. * _xmlSchemaParseTimeZone:
  1303. * @dt: pointer to a date structure
  1304. * @str: pointer to the string to analyze
  1305. *
  1306. * Parses a time zone without time zone and fills in the appropriate
  1307. * field of the @dt structure. @str is updated to point just after the
  1308. * time zone.
  1309. *
  1310. * Returns 0 or the error code
  1311. */
  1312. static int
  1313. _xmlSchemaParseTimeZone (xmlSchemaValDatePtr dt, const xmlChar **str) {
  1314. const xmlChar *cur;
  1315. int ret = 0;
  1316. if (str == NULL)
  1317. return -1;
  1318. cur = *str;
  1319. switch (*cur) {
  1320. case 0:
  1321. dt->tz_flag = 0;
  1322. dt->tzo = 0;
  1323. break;
  1324. case 'Z':
  1325. dt->tz_flag = 1;
  1326. dt->tzo = 0;
  1327. cur++;
  1328. break;
  1329. case '+':
  1330. case '-': {
  1331. int isneg = 0, tmp = 0;
  1332. isneg = (*cur == '-');
  1333. cur++;
  1334. PARSE_2_DIGITS(tmp, cur, ret);
  1335. if (ret != 0)
  1336. return ret;
  1337. if (!VALID_HOUR(tmp))
  1338. return 2;
  1339. if (*cur != ':')
  1340. return 1;
  1341. cur++;
  1342. dt->tzo = tmp * 60;
  1343. PARSE_2_DIGITS(tmp, cur, ret);
  1344. if (ret != 0)
  1345. return ret;
  1346. if (!VALID_MIN(tmp))
  1347. return 2;
  1348. dt->tzo += tmp;
  1349. if (isneg)
  1350. dt->tzo = - dt->tzo;
  1351. if (!VALID_TZO(dt->tzo))
  1352. return 2;
  1353. dt->tz_flag = 1;
  1354. break;
  1355. }
  1356. default:
  1357. return 1;
  1358. }
  1359. *str = cur;
  1360. return 0;
  1361. }
  1362. /**
  1363. * _xmlSchemaBase64Decode:
  1364. * @ch: a character
  1365. *
  1366. * Converts a base64 encoded character to its base 64 value.
  1367. *
  1368. * Returns 0-63 (value), 64 (pad), or -1 (not recognized)
  1369. */
  1370. static int
  1371. _xmlSchemaBase64Decode (const xmlChar ch) {
  1372. if (('A' <= ch) && (ch <= 'Z')) return ch - 'A';
  1373. if (('a' <= ch) && (ch <= 'z')) return ch - 'a' + 26;
  1374. if (('0' <= ch) && (ch <= '9')) return ch - '0' + 52;
  1375. if ('+' == ch) return 62;
  1376. if ('/' == ch) return 63;
  1377. if ('=' == ch) return 64;
  1378. return -1;
  1379. }
  1380. /****************************************************************
  1381. * *
  1382. * XML Schema Dates/Times Datatypes Handling *
  1383. * *
  1384. ****************************************************************/
  1385. /**
  1386. * PARSE_DIGITS:
  1387. * @num: the integer to fill in
  1388. * @cur: an #xmlChar *
  1389. * @num_type: an integer flag
  1390. *
  1391. * Parses a digits integer and updates @num with the value. @cur is
  1392. * updated to point just after the integer.
  1393. * In case of error, @num_type is set to -1, values of @num and
  1394. * @cur are undefined.
  1395. */
  1396. #define PARSE_DIGITS(num, cur, num_type) \
  1397. if ((*cur < '0') || (*cur > '9')) \
  1398. num_type = -1; \
  1399. else \
  1400. while ((*cur >= '0') && (*cur <= '9')) { \
  1401. num = num * 10 + (*cur - '0'); \
  1402. cur++; \
  1403. }
  1404. /**
  1405. * PARSE_NUM:
  1406. * @num: the double to fill in
  1407. * @cur: an #xmlChar *
  1408. * @num_type: an integer flag
  1409. *
  1410. * Parses a float or integer and updates @num with the value. @cur is
  1411. * updated to point just after the number. If the number is a float,
  1412. * then it must have an integer part and a decimal part; @num_type will
  1413. * be set to 1. If there is no decimal part, @num_type is set to zero.
  1414. * In case of error, @num_type is set to -1, values of @num and
  1415. * @cur are undefined.
  1416. */
  1417. #define PARSE_NUM(num, cur, num_type) \
  1418. num = 0; \
  1419. PARSE_DIGITS(num, cur, num_type); \
  1420. if (!num_type && (*cur == '.')) { \
  1421. double mult = 1; \
  1422. cur++; \
  1423. if ((*cur < '0') || (*cur > '9')) \
  1424. num_type = -1; \
  1425. else \
  1426. num_type = 1; \
  1427. while ((*cur >= '0') && (*cur <= '9')) { \
  1428. mult /= 10; \
  1429. num += (*cur - '0') * mult; \
  1430. cur++; \
  1431. } \
  1432. }
  1433. /**
  1434. * xmlSchemaValidateDates:
  1435. * @type: the expected type or XML_SCHEMAS_UNKNOWN
  1436. * @dateTime: string to analyze
  1437. * @val: the return computed value
  1438. *
  1439. * Check that @dateTime conforms to the lexical space of one of the date types.
  1440. * if true a value is computed and returned in @val.
  1441. *
  1442. * Returns 0 if this validates, a positive error code number otherwise
  1443. * and -1 in case of internal or API error.
  1444. */
  1445. static int
  1446. xmlSchemaValidateDates (xmlSchemaValType type,
  1447. const xmlChar *dateTime, xmlSchemaValPtr *val,
  1448. int collapse) {
  1449. xmlSchemaValPtr dt;
  1450. int ret;
  1451. const xmlChar *cur = dateTime;
  1452. #define RETURN_TYPE_IF_VALID(t) \
  1453. if (IS_TZO_CHAR(*cur)) { \
  1454. ret = _xmlSchemaParseTimeZone(&(dt->value.date), &cur); \
  1455. if (ret == 0) { \
  1456. if (*cur != 0) \
  1457. goto error; \
  1458. dt->type = t; \
  1459. goto done; \
  1460. } \
  1461. }
  1462. if (dateTime == NULL)
  1463. return -1;
  1464. if (collapse)
  1465. while IS_WSP_BLANK_CH(*cur) cur++;
  1466. if ((*cur != '-') && (*cur < '0') && (*cur > '9'))
  1467. return 1;
  1468. dt = xmlSchemaNewValue(XML_SCHEMAS_UNKNOWN);
  1469. if (dt == NULL)
  1470. return -1;
  1471. if ((cur[0] == '-') && (cur[1] == '-')) {
  1472. /*
  1473. * It's an incomplete date (xs:gMonthDay, xs:gMonth or
  1474. * xs:gDay)
  1475. */
  1476. cur += 2;
  1477. /* is it an xs:gDay? */
  1478. if (*cur == '-') {
  1479. if (type == XML_SCHEMAS_GMONTH)
  1480. goto error;
  1481. ++cur;
  1482. ret = _xmlSchemaParseGDay(&(dt->value.date), &cur);
  1483. if (ret != 0)
  1484. goto error;
  1485. RETURN_TYPE_IF_VALID(XML_SCHEMAS_GDAY);
  1486. goto error;
  1487. }
  1488. /*
  1489. * it should be an xs:gMonthDay or xs:gMonth
  1490. */
  1491. ret = _xmlSchemaParseGMonth(&(dt->value.date), &cur);
  1492. if (ret != 0)
  1493. goto error;
  1494. /*
  1495. * a '-' char could indicate this type is xs:gMonthDay or
  1496. * a negative time zone offset. Check for xs:gMonthDay first.
  1497. * Also the first three char's of a negative tzo (-MM:SS) can
  1498. * appear to be a valid day; so even if the day portion
  1499. * of the xs:gMonthDay verifies, we must insure it was not
  1500. * a tzo.
  1501. */
  1502. if (*cur == '-') {
  1503. const xmlChar *rewnd = cur;
  1504. cur++;
  1505. ret = _xmlSchemaParseGDay(&(dt->value.date), &cur);
  1506. if ((ret == 0) && ((*cur == 0) || (*cur != ':'))) {
  1507. /*
  1508. * we can use the VALID_MDAY macro to validate the month
  1509. * and day because the leap year test will flag year zero
  1510. * as a leap year (even though zero is an invalid year).
  1511. * FUTURE TODO: Zero will become valid in XML Schema 1.1
  1512. * probably.
  1513. */
  1514. if (VALID_MDAY((&(dt->value.date)))) {
  1515. RETURN_TYPE_IF_VALID(XML_SCHEMAS_GMONTHDAY);
  1516. goto error;
  1517. }
  1518. }
  1519. /*
  1520. * not xs:gMonthDay so rewind and check if just xs:gMonth
  1521. * with an optional time zone.
  1522. */
  1523. cur = rewnd;
  1524. }
  1525. RETURN_TYPE_IF_VALID(XML_SCHEMAS_GMONTH);
  1526. goto error;
  1527. }
  1528. /*
  1529. * It's a right-truncated date or an xs:time.
  1530. * Try to parse an xs:time then fallback on right-truncated dates.
  1531. */
  1532. if ((*cur >= '0') && (*cur <= '9')) {
  1533. ret = _xmlSchemaParseTime(&(dt->value.date), &cur);
  1534. if (ret == 0) {
  1535. /* it's an xs:time */
  1536. RETURN_TYPE_IF_VALID(XML_SCHEMAS_TIME);
  1537. }
  1538. }
  1539. /* fallback on date parsing */
  1540. cur = dateTime;
  1541. ret = _xmlSchemaParseGYear(&(dt->value.date), &cur);
  1542. if (ret != 0)
  1543. goto error;
  1544. /* is it an xs:gYear? */
  1545. RETURN_TYPE_IF_VALID(XML_SCHEMAS_GYEAR);
  1546. if (*cur != '-')
  1547. goto error;
  1548. cur++;
  1549. ret = _xmlSchemaParseGMonth(&(dt->value.date), &cur);
  1550. if (ret != 0)
  1551. goto error;
  1552. /* is it an xs:gYearMonth? */
  1553. RETURN_TYPE_IF_VALID(XML_SCHEMAS_GYEARMONTH);
  1554. if (*cur != '-')
  1555. goto error;
  1556. cur++;
  1557. ret = _xmlSchemaParseGDay(&(dt->value.date), &cur);
  1558. if ((ret != 0) || !VALID_DATE((&(dt->value.date))))
  1559. goto error;
  1560. /* is it an xs:date? */
  1561. RETURN_TYPE_IF_VALID(XML_SCHEMAS_DATE);
  1562. if (*cur != 'T')
  1563. goto error;
  1564. cur++;
  1565. /* it should be an xs:dateTime */
  1566. ret = _xmlSchemaParseTime(&(dt->value.date), &cur);
  1567. if (ret != 0)
  1568. goto error;
  1569. ret = _xmlSchemaParseTimeZone(&(dt->value.date), &cur);
  1570. if (collapse)
  1571. while IS_WSP_BLANK_CH(*cur) cur++;
  1572. if ((ret != 0) || (*cur != 0) || (!(VALID_DATETIME((&(dt->value.date))))))
  1573. goto error;
  1574. dt->type = XML_SCHEMAS_DATETIME;
  1575. done:
  1576. #if 1
  1577. if ((type != XML_SCHEMAS_UNKNOWN) && (type != dt->type))
  1578. goto error;
  1579. #else
  1580. /*
  1581. * insure the parsed type is equal to or less significant (right
  1582. * truncated) than the desired type.
  1583. */
  1584. if ((type != XML_SCHEMAS_UNKNOWN) && (type != dt->type)) {
  1585. /* time only matches time */
  1586. if ((type == XML_SCHEMAS_TIME) && (dt->type == XML_SCHEMAS_TIME))
  1587. goto error;
  1588. if ((type == XML_SCHEMAS_DATETIME) &&
  1589. ((dt->type != XML_SCHEMAS_DATE) ||
  1590. (dt->type != XML_SCHEMAS_GYEARMONTH) ||
  1591. (dt->type != XML_SCHEMAS_GYEAR)))
  1592. goto error;
  1593. if ((type == XML_SCHEMAS_DATE) &&
  1594. ((dt->type != XML_SCHEMAS_GYEAR) ||
  1595. (dt->type != XML_SCHEMAS_GYEARMONTH)))
  1596. goto error;
  1597. if ((type == XML_SCHEMAS_GYEARMONTH) && (dt->type != XML_SCHEMAS_GYEAR))
  1598. goto error;
  1599. if ((type == XML_SCHEMAS_GMONTHDAY) && (dt->type != XML_SCHEMAS_GMONTH))
  1600. goto error;
  1601. }
  1602. #endif
  1603. if (val != NULL)
  1604. *val = dt;
  1605. else
  1606. xmlSchemaFreeValue(dt);
  1607. return 0;
  1608. error:
  1609. if (dt != NULL)
  1610. xmlSchemaFreeValue(dt);
  1611. return 1;
  1612. }
  1613. /**
  1614. * xmlSchemaValidateDuration:
  1615. * @type: the predefined type
  1616. * @duration: string to analyze
  1617. * @val: the return computed value
  1618. *
  1619. * Check that @duration conforms to the lexical space of the duration type.
  1620. * if true a value is computed and returned in @val.
  1621. *
  1622. * Returns 0 if this validates, a positive error code number otherwise
  1623. * and -1 in case of internal or API error.
  1624. */
  1625. static int
  1626. xmlSchemaValidateDuration (xmlSchemaTypePtr type ATTRIBUTE_UNUSED,
  1627. const xmlChar *duration, xmlSchemaValPtr *val,
  1628. int collapse) {
  1629. const xmlChar *cur = duration;
  1630. xmlSchemaValPtr dur;
  1631. int isneg = 0;
  1632. unsigned int seq = 0;
  1633. long days, secs = 0;
  1634. double sec_frac = 0.0;
  1635. if (duration == NULL)
  1636. return -1;
  1637. if (collapse)
  1638. while IS_WSP_BLANK_CH(*cur) cur++;
  1639. if (*cur == '-') {
  1640. isneg = 1;
  1641. cur++;
  1642. }
  1643. /* duration must start with 'P' (after sign) */
  1644. if (*cur++ != 'P')
  1645. return 1;
  1646. if (*cur == 0)
  1647. return 1;
  1648. dur = xmlSchemaNewValue(XML_SCHEMAS_DURATION);
  1649. if (dur == NULL)
  1650. return -1;
  1651. while (*cur != 0) {
  1652. long num = 0;
  1653. size_t has_digits = 0;
  1654. int has_frac = 0;
  1655. const xmlChar desig[] = {'Y', 'M', 'D', 'H', 'M', 'S'};
  1656. /* input string should be empty or invalid date/time item */
  1657. if (seq >= sizeof(desig))
  1658. goto error;
  1659. /* T designator must be present for time items */
  1660. if (*cur == 'T') {
  1661. if (seq > 3)
  1662. goto error;
  1663. cur++;
  1664. seq = 3;
  1665. } else if (seq == 3)
  1666. goto error;
  1667. /* Parse integral part. */
  1668. while (*cur >= '0' && *cur <= '9') {
  1669. long digit = *cur - '0';
  1670. if (num > LONG_MAX / 10)
  1671. goto error;
  1672. num *= 10;
  1673. if (num > LONG_MAX - digit)
  1674. goto error;
  1675. num += digit;
  1676. has_digits = 1;
  1677. cur++;
  1678. }
  1679. if (*cur == '.') {
  1680. /* Parse fractional part. */
  1681. double mult = 1.0;
  1682. cur++;
  1683. has_frac = 1;
  1684. while (*cur >= '0' && *cur <= '9') {
  1685. mult /= 10.0;
  1686. sec_frac += (*cur - '0') * mult;
  1687. has_digits = 1;
  1688. cur++;
  1689. }
  1690. }
  1691. while (*cur != desig[seq]) {
  1692. seq++;
  1693. /* No T designator or invalid char. */
  1694. if (seq == 3 || seq == sizeof(desig))
  1695. goto error;
  1696. }
  1697. cur++;
  1698. if (!has_digits || (has_frac && (seq != 5)))
  1699. goto error;
  1700. switch (seq) {
  1701. case 0:
  1702. /* Year */
  1703. if (num > LONG_MAX / 12)
  1704. goto error;
  1705. dur->value.dur.mon = num * 12;
  1706. break;
  1707. case 1:
  1708. /* Month */
  1709. if (dur->value.dur.mon > LONG_MAX - num)
  1710. goto error;
  1711. dur->value.dur.mon += num;
  1712. break;
  1713. case 2:
  1714. /* Day */
  1715. dur->value.dur.day = num;
  1716. break;
  1717. case 3:
  1718. /* Hour */
  1719. days = num / HOURS_PER_DAY;
  1720. if (dur->value.dur.day > LONG_MAX - days)
  1721. goto error;
  1722. dur->value.dur.day += days;
  1723. secs = (num % HOURS_PER_DAY) * SECS_PER_HOUR;
  1724. break;
  1725. case 4:
  1726. /* Minute */
  1727. days = num / MINS_PER_DAY;
  1728. if (dur->value.dur.day > LONG_MAX - days)
  1729. goto error;
  1730. dur->value.dur.day += days;
  1731. secs += (num % MINS_PER_DAY) * SECS_PER_MIN;
  1732. break;
  1733. case 5:
  1734. /* Second */
  1735. days = num / SECS_PER_DAY;
  1736. if (dur->value.dur.day > LONG_MAX - days)
  1737. goto error;
  1738. dur->value.dur.day += days;
  1739. secs += num % SECS_PER_DAY;
  1740. break;
  1741. }
  1742. seq++;
  1743. }
  1744. days = secs / SECS_PER_DAY;
  1745. if (dur->value.dur.day > LONG_MAX - days)
  1746. goto error;
  1747. dur->value.dur.day += days;
  1748. dur->value.dur.sec = (secs % SECS_PER_DAY) + sec_frac;
  1749. if (isneg) {
  1750. dur->value.dur.mon = -dur->value.dur.mon;
  1751. dur->value.dur.day = -dur->value.dur.day;
  1752. dur->value.dur.sec = -dur->value.dur.sec;
  1753. }
  1754. if (val != NULL)
  1755. *val = dur;
  1756. else
  1757. xmlSchemaFreeValue(dur);
  1758. return 0;
  1759. error:
  1760. if (dur != NULL)
  1761. xmlSchemaFreeValue(dur);
  1762. return 1;
  1763. }
  1764. /**
  1765. * xmlSchemaStrip:
  1766. * @value: a value
  1767. *
  1768. * Removes the leading and ending spaces of a string
  1769. *
  1770. * Returns the new string or NULL if no change was required.
  1771. */
  1772. static xmlChar *
  1773. xmlSchemaStrip(const xmlChar *value) {
  1774. const xmlChar *start = value, *end, *f;
  1775. if (value == NULL) return(NULL);
  1776. while ((*start != 0) && (IS_BLANK_CH(*start))) start++;
  1777. end = start;
  1778. while (*end != 0) end++;
  1779. f = end;
  1780. end--;
  1781. while ((end > start) && (IS_BLANK_CH(*end))) end--;
  1782. end++;
  1783. if ((start == value) && (f == end)) return(NULL);
  1784. return(xmlStrndup(start, end - start));
  1785. }
  1786. /**
  1787. * xmlSchemaWhiteSpaceReplace:
  1788. * @value: a value
  1789. *
  1790. * Replaces 0xd, 0x9 and 0xa with a space.
  1791. *
  1792. * Returns the new string or NULL if no change was required.
  1793. */
  1794. xmlChar *
  1795. xmlSchemaWhiteSpaceReplace(const xmlChar *value) {
  1796. const xmlChar *cur = value;
  1797. xmlChar *ret = NULL, *mcur;
  1798. if (value == NULL)
  1799. return(NULL);
  1800. while ((*cur != 0) &&
  1801. (((*cur) != 0xd) && ((*cur) != 0x9) && ((*cur) != 0xa))) {
  1802. cur++;
  1803. }
  1804. if (*cur == 0)
  1805. return (NULL);
  1806. ret = xmlStrdup(value);
  1807. /* TODO FIXME: I guess gcc will bark at this. */
  1808. mcur = (xmlChar *) (ret + (cur - value));
  1809. do {
  1810. if ( ((*mcur) == 0xd) || ((*mcur) == 0x9) || ((*mcur) == 0xa) )
  1811. *mcur = ' ';
  1812. mcur++;
  1813. } while (*mcur != 0);
  1814. return(ret);
  1815. }
  1816. /**
  1817. * xmlSchemaCollapseString:
  1818. * @value: a value
  1819. *
  1820. * Removes and normalize white spaces in the string
  1821. *
  1822. * Returns the new string or NULL if no change was required.
  1823. */
  1824. xmlChar *
  1825. xmlSchemaCollapseString(const xmlChar *value) {
  1826. const xmlChar *start = value, *end, *f;
  1827. xmlChar *g;
  1828. int col = 0;
  1829. if (value == NULL) return(NULL);
  1830. while ((*start != 0) && (IS_BLANK_CH(*start))) start++;
  1831. end = start;
  1832. while (*end != 0) {
  1833. if ((*end == ' ') && (IS_BLANK_CH(end[1]))) {
  1834. col = end - start;
  1835. break;
  1836. } else if ((*end == 0xa) || (*end == 0x9) || (*end == 0xd)) {
  1837. col = end - start;
  1838. break;
  1839. }
  1840. end++;
  1841. }
  1842. if (col == 0) {
  1843. f = end;
  1844. end--;
  1845. while ((end > start) && (IS_BLANK_CH(*end))) end--;
  1846. end++;
  1847. if ((start == value) && (f == end)) return(NULL);
  1848. return(xmlStrndup(start, end - start));
  1849. }
  1850. start = xmlStrdup(start);
  1851. if (start == NULL) return(NULL);
  1852. g = (xmlChar *) (start + col);
  1853. end = g;
  1854. while (*end != 0) {
  1855. if (IS_BLANK_CH(*end)) {
  1856. end++;
  1857. while (IS_BLANK_CH(*end)) end++;
  1858. if (*end != 0)
  1859. *g++ = ' ';
  1860. } else
  1861. *g++ = *end++;
  1862. }
  1863. *g = 0;
  1864. return((xmlChar *) start);
  1865. }
  1866. /**
  1867. * xmlSchemaValAtomicListNode:
  1868. * @type: the predefined atomic type for a token in the list
  1869. * @value: the list value to check
  1870. * @ret: the return computed value
  1871. * @node: the node containing the value
  1872. *
  1873. * Check that a value conforms to the lexical space of the predefined
  1874. * list type. if true a value is computed and returned in @ret.
  1875. *
  1876. * Returns the number of items if this validates, a negative error code
  1877. * number otherwise
  1878. */
  1879. static int
  1880. xmlSchemaValAtomicListNode(xmlSchemaTypePtr type, const xmlChar *value,
  1881. xmlSchemaValPtr *ret, xmlNodePtr node) {
  1882. xmlChar *val, *cur, *endval;
  1883. int nb_values = 0;
  1884. int tmp = 0;
  1885. if (value == NULL) {
  1886. return(-1);
  1887. }
  1888. val = xmlStrdup(value);
  1889. if (val == NULL) {
  1890. return(-1);
  1891. }
  1892. if (ret != NULL) {
  1893. *ret = NULL;
  1894. }
  1895. cur = val;
  1896. /*
  1897. * Split the list
  1898. */
  1899. while (IS_BLANK_CH(*cur)) *cur++ = 0;
  1900. while (*cur != 0) {
  1901. if (IS_BLANK_CH(*cur)) {
  1902. *cur = 0;
  1903. cur++;
  1904. while (IS_BLANK_CH(*cur)) *cur++ = 0;
  1905. } else {
  1906. nb_values++;
  1907. cur++;
  1908. while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++;
  1909. }
  1910. }
  1911. if (nb_values == 0) {
  1912. xmlFree(val);
  1913. return(nb_values);
  1914. }
  1915. endval = cur;
  1916. cur = val;
  1917. while ((*cur == 0) && (cur != endval)) cur++;
  1918. while (cur != endval) {
  1919. tmp = xmlSchemaValPredefTypeNode(type, cur, NULL, node);
  1920. if (tmp != 0)
  1921. break;
  1922. while (*cur != 0) cur++;
  1923. while ((*cur == 0) && (cur != endval)) cur++;
  1924. }
  1925. /* TODO what return value ? c.f. bug #158628
  1926. if (ret != NULL) {
  1927. TODO
  1928. } */
  1929. xmlFree(val);
  1930. if (tmp == 0)
  1931. return(nb_values);
  1932. return(-1);
  1933. }
  1934. /**
  1935. * xmlSchemaParseUInt:
  1936. * @str: pointer to the string R/W
  1937. * @llo: pointer to the low result
  1938. * @lmi: pointer to the mid result
  1939. * @lhi: pointer to the high result
  1940. *
  1941. * Parse an unsigned long into 3 fields.
  1942. *
  1943. * Returns the number of significant digits in the number or
  1944. * -1 if overflow of the capacity and -2 if it's not a number.
  1945. */
  1946. static int
  1947. xmlSchemaParseUInt(const xmlChar **str, unsigned long *llo,
  1948. unsigned long *lmi, unsigned long *lhi) {
  1949. unsigned long lo = 0, mi = 0, hi = 0;
  1950. const xmlChar *tmp, *cur = *str;
  1951. int ret = 0, i = 0;
  1952. if (!((*cur >= '0') && (*cur <= '9')))
  1953. return(-2);
  1954. while (*cur == '0') { /* ignore leading zeroes */
  1955. cur++;
  1956. }
  1957. tmp = cur;
  1958. while ((*tmp != 0) && (*tmp >= '0') && (*tmp <= '9')) {
  1959. i++;tmp++;ret++;
  1960. }
  1961. if (i > 24) {
  1962. *str = tmp;
  1963. return(-1);
  1964. }
  1965. while (i > 16) {
  1966. hi = hi * 10 + (*cur++ - '0');
  1967. i--;
  1968. }
  1969. while (i > 8) {
  1970. mi = mi * 10 + (*cur++ - '0');
  1971. i--;
  1972. }
  1973. while (i > 0) {
  1974. lo = lo * 10 + (*cur++ - '0');
  1975. i--;
  1976. }
  1977. *str = cur;
  1978. *llo = lo;
  1979. *lmi = mi;
  1980. *lhi = hi;
  1981. return(ret);
  1982. }
  1983. /*
  1984. * xmlSchemaCheckLanguageType
  1985. * @value: the value to check
  1986. *
  1987. * Check that a value conforms to the lexical space of the language datatype.
  1988. * Must conform to [a-zA-Z]{1,8}(-[a-zA-Z0-9]{1,8})*
  1989. *
  1990. * Returns 1 if this validates, 0 otherwise.
  1991. */
  1992. static int
  1993. xmlSchemaCheckLanguageType(const xmlChar* value) {
  1994. int first = 1, len = 0;
  1995. const xmlChar* cur = value;
  1996. if (value == NULL)
  1997. return (0);
  1998. while (cur[0] != 0) {
  1999. if (!( ((cur[0] >= 'a') && (cur[0] <= 'z')) || ((cur[0] >= 'A') && (cur[0] <= 'Z'))
  2000. || (cur[0] == '-')
  2001. || ((first == 0) && (xmlIsDigit_ch(cur[0]))) ))
  2002. return (0);
  2003. if (cur[0] == '-') {
  2004. if ((len < 1) || (len > 8))
  2005. return (0);
  2006. len = 0;
  2007. first = 0;
  2008. }
  2009. else
  2010. len++;
  2011. cur++;
  2012. }
  2013. if ((len < 1) || (len > 8))
  2014. return (0);
  2015. return (1);
  2016. }
  2017. /**
  2018. * xmlSchemaValAtomicType:
  2019. * @type: the predefined type
  2020. * @value: the value to check
  2021. * @val: the return computed value
  2022. * @node: the node containing the value
  2023. * flags: flags to control the validation
  2024. *
  2025. * Check that a value conforms to the lexical space of the atomic type.
  2026. * if true a value is computed and returned in @val.
  2027. * This checks the value space for list types as well (IDREFS, NMTOKENS).
  2028. *
  2029. * Returns 0 if this validates, a positive error code number otherwise
  2030. * and -1 in case of internal or API error.
  2031. */
  2032. static int
  2033. xmlSchemaValAtomicType(xmlSchemaTypePtr type, const xmlChar * value,
  2034. xmlSchemaValPtr * val, xmlNodePtr node, int flags,
  2035. xmlSchemaWhitespaceValueType ws,
  2036. int normOnTheFly, int applyNorm, int createStringValue)
  2037. {
  2038. xmlSchemaValPtr v;
  2039. xmlChar *norm = NULL;
  2040. int ret = 0;
  2041. if (xmlSchemaTypesInitialized == 0)
  2042. xmlSchemaInitTypes();
  2043. if (type == NULL)
  2044. return (-1);
  2045. /*
  2046. * validating a non existent text node is similar to validating
  2047. * an empty one.
  2048. */
  2049. if (value == NULL)
  2050. value = BAD_CAST "";
  2051. if (val != NULL)
  2052. *val = NULL;
  2053. if ((flags == 0) && (value != NULL)) {
  2054. if ((type->builtInType != XML_SCHEMAS_STRING) &&
  2055. (type->builtInType != XML_SCHEMAS_ANYTYPE) &&
  2056. (type->builtInType != XML_SCHEMAS_ANYSIMPLETYPE)) {
  2057. if (type->builtInType == XML_SCHEMAS_NORMSTRING)
  2058. norm = xmlSchemaWhiteSpaceReplace(value);
  2059. else
  2060. norm = xmlSchemaCollapseString(value);
  2061. if (norm != NULL)
  2062. value = norm;
  2063. }
  2064. }
  2065. switch (type->builtInType) {
  2066. case XML_SCHEMAS_UNKNOWN:
  2067. goto error;
  2068. case XML_SCHEMAS_ANYTYPE:
  2069. case XML_SCHEMAS_ANYSIMPLETYPE:
  2070. if ((createStringValue) && (val != NULL)) {
  2071. v = xmlSchemaNewValue(XML_SCHEMAS_ANYSIMPLETYPE);
  2072. if (v != NULL) {
  2073. v->value.str = xmlStrdup(value);
  2074. *val = v;
  2075. } else {
  2076. goto error;
  2077. }
  2078. }
  2079. goto return0;
  2080. case XML_SCHEMAS_STRING:
  2081. if (! normOnTheFly) {
  2082. const xmlChar *cur = value;
  2083. if (ws == XML_SCHEMA_WHITESPACE_REPLACE) {
  2084. while (*cur != 0) {
  2085. if ((*cur == 0xd) || (*cur == 0xa) || (*cur == 0x9)) {
  2086. goto return1;
  2087. } else {
  2088. cur++;
  2089. }
  2090. }
  2091. } else if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE) {
  2092. while (*cur != 0) {
  2093. if ((*cur == 0xd) || (*cur == 0xa) || (*cur == 0x9)) {
  2094. goto return1;
  2095. } else if IS_WSP_SPACE_CH(*cur) {
  2096. cur++;
  2097. if IS_WSP_SPACE_CH(*cur)
  2098. goto return1;
  2099. } else {
  2100. cur++;
  2101. }
  2102. }
  2103. }
  2104. }
  2105. if (createStringValue && (val != NULL)) {
  2106. if (applyNorm) {
  2107. if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
  2108. norm = xmlSchemaCollapseString(value);
  2109. else if (ws == XML_SCHEMA_WHITESPACE_REPLACE)
  2110. norm = xmlSchemaWhiteSpaceReplace(value);
  2111. if (norm != NULL)
  2112. value = norm;
  2113. }
  2114. v = xmlSchemaNewValue(XML_SCHEMAS_STRING);
  2115. if (v != NULL) {
  2116. v->value.str = xmlStrdup(value);
  2117. *val = v;
  2118. } else {
  2119. goto error;
  2120. }
  2121. }
  2122. goto return0;
  2123. case XML_SCHEMAS_NORMSTRING:{
  2124. if (normOnTheFly) {
  2125. if (applyNorm) {
  2126. if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
  2127. norm = xmlSchemaCollapseString(value);
  2128. else
  2129. norm = xmlSchemaWhiteSpaceReplace(value);
  2130. if (norm != NULL)
  2131. value = norm;
  2132. }
  2133. } else {
  2134. const xmlChar *cur = value;
  2135. while (*cur != 0) {
  2136. if ((*cur == 0xd) || (*cur == 0xa) || (*cur == 0x9)) {
  2137. goto return1;
  2138. } else {
  2139. cur++;
  2140. }
  2141. }
  2142. }
  2143. if (val != NULL) {
  2144. v = xmlSchemaNewValue(XML_SCHEMAS_NORMSTRING);
  2145. if (v != NULL) {
  2146. v->value.str = xmlStrdup(value);
  2147. *val = v;
  2148. } else {
  2149. goto error;
  2150. }
  2151. }
  2152. goto return0;
  2153. }
  2154. case XML_SCHEMAS_DECIMAL:{
  2155. const xmlChar *cur = value;
  2156. unsigned int len, neg, integ, hasLeadingZeroes;
  2157. xmlChar cval[25];
  2158. xmlChar *cptr = cval;
  2159. if ((cur == NULL) || (*cur == 0))
  2160. goto return1;
  2161. /*
  2162. * xs:decimal has a whitespace-facet value of 'collapse'.
  2163. */
  2164. if (normOnTheFly)
  2165. while IS_WSP_BLANK_CH(*cur) cur++;
  2166. /*
  2167. * First we handle an optional sign.
  2168. */
  2169. neg = 0;
  2170. if (*cur == '-') {
  2171. neg = 1;
  2172. cur++;
  2173. } else if (*cur == '+')
  2174. cur++;
  2175. /*
  2176. * Disallow: "", "-", "- "
  2177. */
  2178. if (*cur == 0)
  2179. goto return1;
  2180. /*
  2181. * Next we "pre-parse" the number, in preparation for calling
  2182. * the common routine xmlSchemaParseUInt. We get rid of any
  2183. * leading zeroes (because we have reserved only 25 chars),
  2184. * and note the position of a decimal point.
  2185. */
  2186. len = 0;
  2187. integ = ~0u;
  2188. hasLeadingZeroes = 0;
  2189. /*
  2190. * Skip leading zeroes.
  2191. */
  2192. while (*cur == '0') {
  2193. cur++;
  2194. hasLeadingZeroes = 1;
  2195. }
  2196. if (*cur != 0) {
  2197. do {
  2198. if ((*cur >= '0') && (*cur <= '9')) {
  2199. *cptr++ = *cur++;
  2200. len++;
  2201. } else if (*cur == '.') {
  2202. cur++;
  2203. integ = len;
  2204. do {
  2205. if ((*cur >= '0') && (*cur <= '9')) {
  2206. *cptr++ = *cur++;
  2207. len++;
  2208. } else
  2209. break;
  2210. } while (len < 24);
  2211. /*
  2212. * Disallow "." but allow "00."
  2213. */
  2214. if ((len == 0) && (!hasLeadingZeroes))
  2215. goto return1;
  2216. break;
  2217. } else
  2218. break;
  2219. } while (len < 24);
  2220. }
  2221. if (normOnTheFly)
  2222. while IS_WSP_BLANK_CH(*cur) cur++;
  2223. if (*cur != 0)
  2224. goto return1; /* error if any extraneous chars */
  2225. if (val != NULL) {
  2226. v = xmlSchemaNewValue(XML_SCHEMAS_DECIMAL);
  2227. if (v != NULL) {
  2228. /*
  2229. * Now evaluate the significant digits of the number
  2230. */
  2231. if (len != 0) {
  2232. if (integ != ~0u) {
  2233. /*
  2234. * Get rid of trailing zeroes in the
  2235. * fractional part.
  2236. */
  2237. while ((len != integ) && (*(cptr-1) == '0')) {
  2238. cptr--;
  2239. len--;
  2240. }
  2241. }
  2242. /*
  2243. * Terminate the (preparsed) string.
  2244. */
  2245. if (len != 0) {
  2246. *cptr = 0;
  2247. cptr = cval;
  2248. xmlSchemaParseUInt((const xmlChar **)&cptr,
  2249. &v->value.decimal.lo,
  2250. &v->value.decimal.mi,
  2251. &v->value.decimal.hi);
  2252. }
  2253. }
  2254. /*
  2255. * Set the total digits to 1 if a zero value.
  2256. */
  2257. v->value.decimal.sign = neg;
  2258. if (len == 0) {
  2259. /* Speedup for zero values. */
  2260. v->value.decimal.total = 1;
  2261. } else {
  2262. v->value.decimal.total = len;
  2263. if (integ == ~0u)
  2264. v->value.decimal.frac = 0;
  2265. else
  2266. v->value.decimal.frac = len - integ;
  2267. }
  2268. *val = v;
  2269. }
  2270. }
  2271. goto return0;
  2272. }
  2273. case XML_SCHEMAS_TIME:
  2274. case XML_SCHEMAS_GDAY:
  2275. case XML_SCHEMAS_GMONTH:
  2276. case XML_SCHEMAS_GMONTHDAY:
  2277. case XML_SCHEMAS_GYEAR:
  2278. case XML_SCHEMAS_GYEARMONTH:
  2279. case XML_SCHEMAS_DATE:
  2280. case XML_SCHEMAS_DATETIME:
  2281. ret = xmlSchemaValidateDates(type->builtInType, value, val,
  2282. normOnTheFly);
  2283. break;
  2284. case XML_SCHEMAS_DURATION:
  2285. ret = xmlSchemaValidateDuration(type, value, val,
  2286. normOnTheFly);
  2287. break;
  2288. case XML_SCHEMAS_FLOAT:
  2289. case XML_SCHEMAS_DOUBLE: {
  2290. const xmlChar *cur = value;
  2291. int neg = 0;
  2292. int digits_before = 0;
  2293. int digits_after = 0;
  2294. if (normOnTheFly)
  2295. while IS_WSP_BLANK_CH(*cur) cur++;
  2296. if ((cur[0] == 'N') && (cur[1] == 'a') && (cur[2] == 'N')) {
  2297. cur += 3;
  2298. if (*cur != 0)
  2299. goto return1;
  2300. if (val != NULL) {
  2301. if (type == xmlSchemaTypeFloatDef) {
  2302. v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT);
  2303. if (v != NULL) {
  2304. v->value.f = (float) xmlXPathNAN;
  2305. } else {
  2306. xmlSchemaFreeValue(v);
  2307. goto error;
  2308. }
  2309. } else {
  2310. v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE);
  2311. if (v != NULL) {
  2312. v->value.d = xmlXPathNAN;
  2313. } else {
  2314. xmlSchemaFreeValue(v);
  2315. goto error;
  2316. }
  2317. }
  2318. *val = v;
  2319. }
  2320. goto return0;
  2321. }
  2322. if (*cur == '-') {
  2323. neg = 1;
  2324. cur++;
  2325. }
  2326. if ((cur[0] == 'I') && (cur[1] == 'N') && (cur[2] == 'F')) {
  2327. cur += 3;
  2328. if (*cur != 0)
  2329. goto return1;
  2330. if (val != NULL) {
  2331. if (type == xmlSchemaTypeFloatDef) {
  2332. v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT);
  2333. if (v != NULL) {
  2334. if (neg)
  2335. v->value.f = (float) xmlXPathNINF;
  2336. else
  2337. v->value.f = (float) xmlXPathPINF;
  2338. } else {
  2339. xmlSchemaFreeValue(v);
  2340. goto error;
  2341. }
  2342. } else {
  2343. v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE);
  2344. if (v != NULL) {
  2345. if (neg)
  2346. v->value.d = xmlXPathNINF;
  2347. else
  2348. v->value.d = xmlXPathPINF;
  2349. } else {
  2350. xmlSchemaFreeValue(v);
  2351. goto error;
  2352. }
  2353. }
  2354. *val = v;
  2355. }
  2356. goto return0;
  2357. }
  2358. if ((neg == 0) && (*cur == '+'))
  2359. cur++;
  2360. if ((cur[0] == 0) || (cur[0] == '+') || (cur[0] == '-'))
  2361. goto return1;
  2362. while ((*cur >= '0') && (*cur <= '9')) {
  2363. cur++;
  2364. digits_before++;
  2365. }
  2366. if (*cur == '.') {
  2367. cur++;
  2368. while ((*cur >= '0') && (*cur <= '9')) {
  2369. cur++;
  2370. digits_after++;
  2371. }
  2372. }
  2373. if ((digits_before == 0) && (digits_after == 0))
  2374. goto return1;
  2375. if ((*cur == 'e') || (*cur == 'E')) {
  2376. cur++;
  2377. if ((*cur == '-') || (*cur == '+'))
  2378. cur++;
  2379. while ((*cur >= '0') && (*cur <= '9'))
  2380. cur++;
  2381. }
  2382. if (normOnTheFly)
  2383. while IS_WSP_BLANK_CH(*cur) cur++;
  2384. if (*cur != 0)
  2385. goto return1;
  2386. if (val != NULL) {
  2387. if (type == xmlSchemaTypeFloatDef) {
  2388. v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT);
  2389. if (v != NULL) {
  2390. /*
  2391. * TODO: sscanf seems not to give the correct
  2392. * value for extremely high/low values.
  2393. * E.g. "1E-149" results in zero.
  2394. */
  2395. if (sscanf((const char *) value, "%f",
  2396. &(v->value.f)) == 1) {
  2397. *val = v;
  2398. } else {
  2399. xmlSchemaFreeValue(v);
  2400. goto return1;
  2401. }
  2402. } else {
  2403. goto error;
  2404. }
  2405. } else {
  2406. v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE);
  2407. if (v != NULL) {
  2408. /*
  2409. * TODO: sscanf seems not to give the correct
  2410. * value for extremely high/low values.
  2411. */
  2412. if (sscanf((const char *) value, "%lf",
  2413. &(v->value.d)) == 1) {
  2414. *val = v;
  2415. } else {
  2416. xmlSchemaFreeValue(v);
  2417. goto return1;
  2418. }
  2419. } else {
  2420. goto error;
  2421. }
  2422. }
  2423. }
  2424. goto return0;
  2425. }
  2426. case XML_SCHEMAS_BOOLEAN:{
  2427. const xmlChar *cur = value;
  2428. if (normOnTheFly) {
  2429. while IS_WSP_BLANK_CH(*cur) cur++;
  2430. if (*cur == '0') {
  2431. ret = 0;
  2432. cur++;
  2433. } else if (*cur == '1') {
  2434. ret = 1;
  2435. cur++;
  2436. } else if (*cur == 't') {
  2437. cur++;
  2438. if ((*cur++ == 'r') && (*cur++ == 'u') &&
  2439. (*cur++ == 'e')) {
  2440. ret = 1;
  2441. } else
  2442. goto return1;
  2443. } else if (*cur == 'f') {
  2444. cur++;
  2445. if ((*cur++ == 'a') && (*cur++ == 'l') &&
  2446. (*cur++ == 's') && (*cur++ == 'e')) {
  2447. ret = 0;
  2448. } else
  2449. goto return1;
  2450. } else
  2451. goto return1;
  2452. if (*cur != 0) {
  2453. while IS_WSP_BLANK_CH(*cur) cur++;
  2454. if (*cur != 0)
  2455. goto return1;
  2456. }
  2457. } else {
  2458. if ((cur[0] == '0') && (cur[1] == 0))
  2459. ret = 0;
  2460. else if ((cur[0] == '1') && (cur[1] == 0))
  2461. ret = 1;
  2462. else if ((cur[0] == 't') && (cur[1] == 'r')
  2463. && (cur[2] == 'u') && (cur[3] == 'e')
  2464. && (cur[4] == 0))
  2465. ret = 1;
  2466. else if ((cur[0] == 'f') && (cur[1] == 'a')
  2467. && (cur[2] == 'l') && (cur[3] == 's')
  2468. && (cur[4] == 'e') && (cur[5] == 0))
  2469. ret = 0;
  2470. else
  2471. goto return1;
  2472. }
  2473. if (val != NULL) {
  2474. v = xmlSchemaNewValue(XML_SCHEMAS_BOOLEAN);
  2475. if (v != NULL) {
  2476. v->value.b = ret;
  2477. *val = v;
  2478. } else {
  2479. goto error;
  2480. }
  2481. }
  2482. goto return0;
  2483. }
  2484. case XML_SCHEMAS_TOKEN:{
  2485. const xmlChar *cur = value;
  2486. if (! normOnTheFly) {
  2487. while (*cur != 0) {
  2488. if ((*cur == 0xd) || (*cur == 0xa) || (*cur == 0x9)) {
  2489. goto return1;
  2490. } else if (*cur == ' ') {
  2491. cur++;
  2492. if (*cur == 0)
  2493. goto return1;
  2494. if (*cur == ' ')
  2495. goto return1;
  2496. } else {
  2497. cur++;
  2498. }
  2499. }
  2500. }
  2501. if (val != NULL) {
  2502. v = xmlSchemaNewValue(XML_SCHEMAS_TOKEN);
  2503. if (v != NULL) {
  2504. v->value.str = xmlStrdup(value);
  2505. *val = v;
  2506. } else {
  2507. goto error;
  2508. }
  2509. }
  2510. goto return0;
  2511. }
  2512. case XML_SCHEMAS_LANGUAGE:
  2513. if ((norm == NULL) && (normOnTheFly)) {
  2514. norm = xmlSchemaCollapseString(value);
  2515. if (norm != NULL)
  2516. value = norm;
  2517. }
  2518. if (xmlSchemaCheckLanguageType(value) == 1) {
  2519. if (val != NULL) {
  2520. v = xmlSchemaNewValue(XML_SCHEMAS_LANGUAGE);
  2521. if (v != NULL) {
  2522. v->value.str = xmlStrdup(value);
  2523. *val = v;
  2524. } else {
  2525. goto error;
  2526. }
  2527. }
  2528. goto return0;
  2529. }
  2530. goto return1;
  2531. case XML_SCHEMAS_NMTOKEN:
  2532. if (xmlValidateNMToken(value, 1) == 0) {
  2533. if (val != NULL) {
  2534. v = xmlSchemaNewValue(XML_SCHEMAS_NMTOKEN);
  2535. if (v != NULL) {
  2536. v->value.str = xmlStrdup(value);
  2537. *val = v;
  2538. } else {
  2539. goto error;
  2540. }
  2541. }
  2542. goto return0;
  2543. }
  2544. goto return1;
  2545. case XML_SCHEMAS_NMTOKENS:
  2546. ret = xmlSchemaValAtomicListNode(xmlSchemaTypeNmtokenDef,
  2547. value, val, node);
  2548. if (ret > 0)
  2549. ret = 0;
  2550. else
  2551. ret = 1;
  2552. goto done;
  2553. case XML_SCHEMAS_NAME:
  2554. ret = xmlValidateName(value, 1);
  2555. if ((ret == 0) && (val != NULL) && (value != NULL)) {
  2556. v = xmlSchemaNewValue(XML_SCHEMAS_NAME);
  2557. if (v != NULL) {
  2558. const xmlChar *start = value, *end;
  2559. while (IS_BLANK_CH(*start)) start++;
  2560. end = start;
  2561. while ((*end != 0) && (!IS_BLANK_CH(*end))) end++;
  2562. v->value.str = xmlStrndup(start, end - start);
  2563. *val = v;
  2564. } else {
  2565. goto error;
  2566. }
  2567. }
  2568. goto done;
  2569. case XML_SCHEMAS_QNAME:{
  2570. const xmlChar *uri = NULL;
  2571. xmlChar *local = NULL;
  2572. ret = xmlValidateQName(value, 1);
  2573. if (ret != 0)
  2574. goto done;
  2575. if (node != NULL) {
  2576. xmlChar *prefix;
  2577. xmlNsPtr ns;
  2578. local = xmlSplitQName2(value, &prefix);
  2579. ns = xmlSearchNs(node->doc, node, prefix);
  2580. if ((ns == NULL) && (prefix != NULL)) {
  2581. xmlFree(prefix);
  2582. if (local != NULL)
  2583. xmlFree(local);
  2584. goto return1;
  2585. }
  2586. if (ns != NULL)
  2587. uri = ns->href;
  2588. if (prefix != NULL)
  2589. xmlFree(prefix);
  2590. }
  2591. if (val != NULL) {
  2592. v = xmlSchemaNewValue(XML_SCHEMAS_QNAME);
  2593. if (v == NULL) {
  2594. if (local != NULL)
  2595. xmlFree(local);
  2596. goto error;
  2597. }
  2598. if (local != NULL)
  2599. v->value.qname.name = local;
  2600. else
  2601. v->value.qname.name = xmlStrdup(value);
  2602. if (uri != NULL)
  2603. v->value.qname.uri = xmlStrdup(uri);
  2604. *val = v;
  2605. } else
  2606. if (local != NULL)
  2607. xmlFree(local);
  2608. goto done;
  2609. }
  2610. case XML_SCHEMAS_NCNAME:
  2611. ret = xmlValidateNCName(value, 1);
  2612. if ((ret == 0) && (val != NULL)) {
  2613. v = xmlSchemaNewValue(XML_SCHEMAS_NCNAME);
  2614. if (v != NULL) {
  2615. v->value.str = xmlStrdup(value);
  2616. *val = v;
  2617. } else {
  2618. goto error;
  2619. }
  2620. }
  2621. goto done;
  2622. case XML_SCHEMAS_ID:
  2623. ret = xmlValidateNCName(value, 1);
  2624. if ((ret == 0) && (val != NULL)) {
  2625. v = xmlSchemaNewValue(XML_SCHEMAS_ID);
  2626. if (v != NULL) {
  2627. v->value.str = xmlStrdup(value);
  2628. *val = v;
  2629. } else {
  2630. goto error;
  2631. }
  2632. }
  2633. if ((ret == 0) && (node != NULL) &&
  2634. (node->type == XML_ATTRIBUTE_NODE)) {
  2635. xmlAttrPtr attr = (xmlAttrPtr) node;
  2636. /*
  2637. * NOTE: the IDness might have already be declared in the DTD
  2638. */
  2639. if (attr->atype != XML_ATTRIBUTE_ID) {
  2640. xmlIDPtr res;
  2641. xmlChar *strip;
  2642. strip = xmlSchemaStrip(value);
  2643. if (strip != NULL) {
  2644. res = xmlAddID(NULL, node->doc, strip, attr);
  2645. xmlFree(strip);
  2646. } else
  2647. res = xmlAddID(NULL, node->doc, value, attr);
  2648. if (res == NULL) {
  2649. ret = 2;
  2650. } else {
  2651. attr->atype = XML_ATTRIBUTE_ID;
  2652. }
  2653. }
  2654. }
  2655. goto done;
  2656. case XML_SCHEMAS_IDREF:
  2657. ret = xmlValidateNCName(value, 1);
  2658. if ((ret == 0) && (val != NULL)) {
  2659. v = xmlSchemaNewValue(XML_SCHEMAS_IDREF);
  2660. if (v == NULL)
  2661. goto error;
  2662. v->value.str = xmlStrdup(value);
  2663. *val = v;
  2664. }
  2665. if ((ret == 0) && (node != NULL) &&
  2666. (node->type == XML_ATTRIBUTE_NODE)) {
  2667. xmlAttrPtr attr = (xmlAttrPtr) node;
  2668. xmlChar *strip;
  2669. strip = xmlSchemaStrip(value);
  2670. if (strip != NULL) {
  2671. xmlAddRef(NULL, node->doc, strip, attr);
  2672. xmlFree(strip);
  2673. } else
  2674. xmlAddRef(NULL, node->doc, value, attr);
  2675. attr->atype = XML_ATTRIBUTE_IDREF;
  2676. }
  2677. goto done;
  2678. case XML_SCHEMAS_IDREFS:
  2679. ret = xmlSchemaValAtomicListNode(xmlSchemaTypeIdrefDef,
  2680. value, val, node);
  2681. if (ret < 0)
  2682. ret = 2;
  2683. else
  2684. ret = 0;
  2685. if ((ret == 0) && (node != NULL) &&
  2686. (node->type == XML_ATTRIBUTE_NODE)) {
  2687. xmlAttrPtr attr = (xmlAttrPtr) node;
  2688. attr->atype = XML_ATTRIBUTE_IDREFS;
  2689. }
  2690. goto done;
  2691. case XML_SCHEMAS_ENTITY:{
  2692. xmlChar *strip;
  2693. ret = xmlValidateNCName(value, 1);
  2694. if ((node == NULL) || (node->doc == NULL))
  2695. ret = 3;
  2696. if (ret == 0) {
  2697. xmlEntityPtr ent;
  2698. strip = xmlSchemaStrip(value);
  2699. if (strip != NULL) {
  2700. ent = xmlGetDocEntity(node->doc, strip);
  2701. xmlFree(strip);
  2702. } else {
  2703. ent = xmlGetDocEntity(node->doc, value);
  2704. }
  2705. if ((ent == NULL) ||
  2706. (ent->etype !=
  2707. XML_EXTERNAL_GENERAL_UNPARSED_ENTITY))
  2708. ret = 4;
  2709. }
  2710. if ((ret == 0) && (val != NULL)) {
  2711. TODO;
  2712. }
  2713. if ((ret == 0) && (node != NULL) &&
  2714. (node->type == XML_ATTRIBUTE_NODE)) {
  2715. xmlAttrPtr attr = (xmlAttrPtr) node;
  2716. attr->atype = XML_ATTRIBUTE_ENTITY;
  2717. }
  2718. goto done;
  2719. }
  2720. case XML_SCHEMAS_ENTITIES:
  2721. if ((node == NULL) || (node->doc == NULL))
  2722. goto return3;
  2723. ret = xmlSchemaValAtomicListNode(xmlSchemaTypeEntityDef,
  2724. value, val, node);
  2725. if (ret <= 0)
  2726. ret = 1;
  2727. else
  2728. ret = 0;
  2729. if ((ret == 0) && (node != NULL) &&
  2730. (node->type == XML_ATTRIBUTE_NODE)) {
  2731. xmlAttrPtr attr = (xmlAttrPtr) node;
  2732. attr->atype = XML_ATTRIBUTE_ENTITIES;
  2733. }
  2734. goto done;
  2735. case XML_SCHEMAS_NOTATION:{
  2736. xmlChar *uri = NULL;
  2737. xmlChar *local = NULL;
  2738. ret = xmlValidateQName(value, 1);
  2739. if ((ret == 0) && (node != NULL)) {
  2740. xmlChar *prefix;
  2741. local = xmlSplitQName2(value, &prefix);
  2742. if (prefix != NULL) {
  2743. xmlNsPtr ns;
  2744. ns = xmlSearchNs(node->doc, node, prefix);
  2745. if (ns == NULL)
  2746. ret = 1;
  2747. else if (val != NULL)
  2748. uri = xmlStrdup(ns->href);
  2749. }
  2750. if ((local != NULL) && ((val == NULL) || (ret != 0)))
  2751. xmlFree(local);
  2752. if (prefix != NULL)
  2753. xmlFree(prefix);
  2754. }
  2755. if ((node == NULL) || (node->doc == NULL))
  2756. ret = 3;
  2757. if (ret == 0) {
  2758. ret = xmlValidateNotationUse(NULL, node->doc, value);
  2759. if (ret == 1)
  2760. ret = 0;
  2761. else
  2762. ret = 1;
  2763. }
  2764. if ((ret == 0) && (val != NULL)) {
  2765. v = xmlSchemaNewValue(XML_SCHEMAS_NOTATION);
  2766. if (v != NULL) {
  2767. if (local != NULL)
  2768. v->value.qname.name = local;
  2769. else
  2770. v->value.qname.name = xmlStrdup(value);
  2771. if (uri != NULL)
  2772. v->value.qname.uri = uri;
  2773. *val = v;
  2774. } else {
  2775. if (local != NULL)
  2776. xmlFree(local);
  2777. if (uri != NULL)
  2778. xmlFree(uri);
  2779. goto error;
  2780. }
  2781. }
  2782. goto done;
  2783. }
  2784. case XML_SCHEMAS_ANYURI:{
  2785. if (*value != 0) {
  2786. xmlURIPtr uri;
  2787. xmlChar *tmpval, *cur;
  2788. if ((norm == NULL) && (normOnTheFly)) {
  2789. norm = xmlSchemaCollapseString(value);
  2790. if (norm != NULL)
  2791. value = norm;
  2792. }
  2793. tmpval = xmlStrdup(value);
  2794. for (cur = tmpval; *cur; ++cur) {
  2795. if (*cur < 32 || *cur >= 127 || *cur == ' ' ||
  2796. *cur == '<' || *cur == '>' || *cur == '"' ||
  2797. *cur == '{' || *cur == '}' || *cur == '|' ||
  2798. *cur == '\\' || *cur == '^' || *cur == '`' ||
  2799. *cur == '\'')
  2800. *cur = '_';
  2801. }
  2802. uri = xmlParseURI((const char *) tmpval);
  2803. xmlFree(tmpval);
  2804. if (uri == NULL)
  2805. goto return1;
  2806. xmlFreeURI(uri);
  2807. }
  2808. if (val != NULL) {
  2809. v = xmlSchemaNewValue(XML_SCHEMAS_ANYURI);
  2810. if (v == NULL)
  2811. goto error;
  2812. v->value.str = xmlStrdup(value);
  2813. *val = v;
  2814. }
  2815. goto return0;
  2816. }
  2817. case XML_SCHEMAS_HEXBINARY:{
  2818. const xmlChar *cur = value, *start;
  2819. xmlChar *base;
  2820. int total, i = 0;
  2821. if (cur == NULL)
  2822. goto return1;
  2823. if (normOnTheFly)
  2824. while IS_WSP_BLANK_CH(*cur) cur++;
  2825. start = cur;
  2826. while (((*cur >= '0') && (*cur <= '9')) ||
  2827. ((*cur >= 'A') && (*cur <= 'F')) ||
  2828. ((*cur >= 'a') && (*cur <= 'f'))) {
  2829. i++;
  2830. cur++;
  2831. }
  2832. if (normOnTheFly)
  2833. while IS_WSP_BLANK_CH(*cur) cur++;
  2834. if (*cur != 0)
  2835. goto return1;
  2836. if ((i % 2) != 0)
  2837. goto return1;
  2838. if (val != NULL) {
  2839. v = xmlSchemaNewValue(XML_SCHEMAS_HEXBINARY);
  2840. if (v == NULL)
  2841. goto error;
  2842. /*
  2843. * Copy only the normalized piece.
  2844. * CRITICAL TODO: Check this.
  2845. */
  2846. cur = xmlStrndup(start, i);
  2847. if (cur == NULL) {
  2848. xmlSchemaTypeErrMemory(node, "allocating hexbin data");
  2849. xmlFree(v);
  2850. goto return1;
  2851. }
  2852. total = i / 2; /* number of octets */
  2853. base = (xmlChar *) cur;
  2854. while (i-- > 0) {
  2855. if (*base >= 'a')
  2856. *base = *base - ('a' - 'A');
  2857. base++;
  2858. }
  2859. v->value.hex.str = (xmlChar *) cur;
  2860. v->value.hex.total = total;
  2861. *val = v;
  2862. }
  2863. goto return0;
  2864. }
  2865. case XML_SCHEMAS_BASE64BINARY:{
  2866. /* ISSUE:
  2867. *
  2868. * Ignore all stray characters? (yes, currently)
  2869. * Worry about long lines? (no, currently)
  2870. *
  2871. * rfc2045.txt:
  2872. *
  2873. * "The encoded output stream must be represented in lines of
  2874. * no more than 76 characters each. All line breaks or other
  2875. * characters not found in Table 1 must be ignored by decoding
  2876. * software. In base64 data, characters other than those in
  2877. * Table 1, line breaks, and other white space probably
  2878. * indicate a transmission error, about which a warning
  2879. * message or even a message rejection might be appropriate
  2880. * under some circumstances." */
  2881. const xmlChar *cur = value;
  2882. xmlChar *base;
  2883. int total, i = 0, pad = 0;
  2884. if (cur == NULL)
  2885. goto return1;
  2886. for (; *cur; ++cur) {
  2887. int decc;
  2888. decc = _xmlSchemaBase64Decode(*cur);
  2889. if (decc < 0) ;
  2890. else if (decc < 64)
  2891. i++;
  2892. else
  2893. break;
  2894. }
  2895. for (; *cur; ++cur) {
  2896. int decc;
  2897. decc = _xmlSchemaBase64Decode(*cur);
  2898. if (decc < 0) ;
  2899. else if (decc < 64)
  2900. goto return1;
  2901. if (decc == 64)
  2902. pad++;
  2903. }
  2904. /* rfc2045.txt: "Special processing is performed if fewer than
  2905. * 24 bits are available at the end of the data being encoded.
  2906. * A full encoding quantum is always completed at the end of a
  2907. * body. When fewer than 24 input bits are available in an
  2908. * input group, zero bits are added (on the right) to form an
  2909. * integral number of 6-bit groups. Padding at the end of the
  2910. * data is performed using the "=" character. Since all
  2911. * base64 input is an integral number of octets, only the
  2912. * following cases can arise: (1) the final quantum of
  2913. * encoding input is an integral multiple of 24 bits; here,
  2914. * the final unit of encoded output will be an integral
  2915. * multiple of indent: Standard input:701: Warning:old style
  2916. * assignment ambiguity in "=*". Assuming "= *" 4 characters
  2917. * with no "=" padding, (2) the final
  2918. * quantum of encoding input is exactly 8 bits; here, the
  2919. * final unit of encoded output will be two characters
  2920. * followed by two "=" padding characters, or (3) the final
  2921. * quantum of encoding input is exactly 16 bits; here, the
  2922. * final unit of encoded output will be three characters
  2923. * followed by one "=" padding character." */
  2924. total = 3 * (i / 4);
  2925. if (pad == 0) {
  2926. if (i % 4 != 0)
  2927. goto return1;
  2928. } else if (pad == 1) {
  2929. int decc;
  2930. if (i % 4 != 3)
  2931. goto return1;
  2932. for (decc = _xmlSchemaBase64Decode(*cur);
  2933. (decc < 0) || (decc > 63);
  2934. decc = _xmlSchemaBase64Decode(*cur))
  2935. --cur;
  2936. /* 16bits in 24bits means 2 pad bits: nnnnnn nnmmmm mmmm00*/
  2937. /* 00111100 -> 0x3c */
  2938. if (decc & ~0x3c)
  2939. goto return1;
  2940. total += 2;
  2941. } else if (pad == 2) {
  2942. int decc;
  2943. if (i % 4 != 2)
  2944. goto return1;
  2945. for (decc = _xmlSchemaBase64Decode(*cur);
  2946. (decc < 0) || (decc > 63);
  2947. decc = _xmlSchemaBase64Decode(*cur))
  2948. --cur;
  2949. /* 8bits in 12bits means 4 pad bits: nnnnnn nn0000 */
  2950. /* 00110000 -> 0x30 */
  2951. if (decc & ~0x30)
  2952. goto return1;
  2953. total += 1;
  2954. } else
  2955. goto return1;
  2956. if (val != NULL) {
  2957. v = xmlSchemaNewValue(XML_SCHEMAS_BASE64BINARY);
  2958. if (v == NULL)
  2959. goto error;
  2960. base =
  2961. (xmlChar *) xmlMallocAtomic((i + pad + 1) *
  2962. sizeof(xmlChar));
  2963. if (base == NULL) {
  2964. xmlSchemaTypeErrMemory(node, "allocating base64 data");
  2965. xmlFree(v);
  2966. goto return1;
  2967. }
  2968. v->value.base64.str = base;
  2969. for (cur = value; *cur; ++cur)
  2970. if (_xmlSchemaBase64Decode(*cur) >= 0) {
  2971. *base = *cur;
  2972. ++base;
  2973. }
  2974. *base = 0;
  2975. v->value.base64.total = total;
  2976. *val = v;
  2977. }
  2978. goto return0;
  2979. }
  2980. case XML_SCHEMAS_INTEGER:
  2981. case XML_SCHEMAS_PINTEGER:
  2982. case XML_SCHEMAS_NPINTEGER:
  2983. case XML_SCHEMAS_NINTEGER:
  2984. case XML_SCHEMAS_NNINTEGER:{
  2985. const xmlChar *cur = value;
  2986. unsigned long lo, mi, hi;
  2987. int sign = 0;
  2988. if (cur == NULL)
  2989. goto return1;
  2990. if (normOnTheFly)
  2991. while IS_WSP_BLANK_CH(*cur) cur++;
  2992. if (*cur == '-') {
  2993. sign = 1;
  2994. cur++;
  2995. } else if (*cur == '+')
  2996. cur++;
  2997. ret = xmlSchemaParseUInt(&cur, &lo, &mi, &hi);
  2998. if (ret < 0)
  2999. goto return1;
  3000. if (normOnTheFly)
  3001. while IS_WSP_BLANK_CH(*cur) cur++;
  3002. if (*cur != 0)
  3003. goto return1;
  3004. if (type->builtInType == XML_SCHEMAS_NPINTEGER) {
  3005. if ((sign == 0) &&
  3006. ((hi != 0) || (mi != 0) || (lo != 0)))
  3007. goto return1;
  3008. } else if (type->builtInType == XML_SCHEMAS_PINTEGER) {
  3009. if (sign == 1)
  3010. goto return1;
  3011. if ((hi == 0) && (mi == 0) && (lo == 0))
  3012. goto return1;
  3013. } else if (type->builtInType == XML_SCHEMAS_NINTEGER) {
  3014. if (sign == 0)
  3015. goto return1;
  3016. if ((hi == 0) && (mi == 0) && (lo == 0))
  3017. goto return1;
  3018. } else if (type->builtInType == XML_SCHEMAS_NNINTEGER) {
  3019. if ((sign == 1) &&
  3020. ((hi != 0) || (mi != 0) || (lo != 0)))
  3021. goto return1;
  3022. }
  3023. if (val != NULL) {
  3024. v = xmlSchemaNewValue(type->builtInType);
  3025. if (v != NULL) {
  3026. if (ret == 0)
  3027. ret++;
  3028. v->value.decimal.lo = lo;
  3029. v->value.decimal.mi = mi;
  3030. v->value.decimal.hi = hi;
  3031. v->value.decimal.sign = sign;
  3032. v->value.decimal.frac = 0;
  3033. v->value.decimal.total = ret;
  3034. *val = v;
  3035. }
  3036. }
  3037. goto return0;
  3038. }
  3039. case XML_SCHEMAS_LONG:
  3040. case XML_SCHEMAS_BYTE:
  3041. case XML_SCHEMAS_SHORT:
  3042. case XML_SCHEMAS_INT:{
  3043. const xmlChar *cur = value;
  3044. unsigned long lo, mi, hi;
  3045. int sign = 0;
  3046. if (cur == NULL)
  3047. goto return1;
  3048. if (*cur == '-') {
  3049. sign = 1;
  3050. cur++;
  3051. } else if (*cur == '+')
  3052. cur++;
  3053. ret = xmlSchemaParseUInt(&cur, &lo, &mi, &hi);
  3054. if (ret < 0)
  3055. goto return1;
  3056. if (*cur != 0)
  3057. goto return1;
  3058. if (type->builtInType == XML_SCHEMAS_LONG) {
  3059. if (hi >= 922) {
  3060. if (hi > 922)
  3061. goto return1;
  3062. if (mi >= 33720368) {
  3063. if (mi > 33720368)
  3064. goto return1;
  3065. if ((sign == 0) && (lo > 54775807))
  3066. goto return1;
  3067. if ((sign == 1) && (lo > 54775808))
  3068. goto return1;
  3069. }
  3070. }
  3071. } else if (type->builtInType == XML_SCHEMAS_INT) {
  3072. if (hi != 0)
  3073. goto return1;
  3074. if (mi >= 21) {
  3075. if (mi > 21)
  3076. goto return1;
  3077. if ((sign == 0) && (lo > 47483647))
  3078. goto return1;
  3079. if ((sign == 1) && (lo > 47483648))
  3080. goto return1;
  3081. }
  3082. } else if (type->builtInType == XML_SCHEMAS_SHORT) {
  3083. if ((mi != 0) || (hi != 0))
  3084. goto return1;
  3085. if ((sign == 1) && (lo > 32768))
  3086. goto return1;
  3087. if ((sign == 0) && (lo > 32767))
  3088. goto return1;
  3089. } else if (type->builtInType == XML_SCHEMAS_BYTE) {
  3090. if ((mi != 0) || (hi != 0))
  3091. goto return1;
  3092. if ((sign == 1) && (lo > 128))
  3093. goto return1;
  3094. if ((sign == 0) && (lo > 127))
  3095. goto return1;
  3096. }
  3097. if (val != NULL) {
  3098. v = xmlSchemaNewValue(type->builtInType);
  3099. if (v != NULL) {
  3100. v->value.decimal.lo = lo;
  3101. v->value.decimal.mi = mi;
  3102. v->value.decimal.hi = hi;
  3103. v->value.decimal.sign = sign;
  3104. v->value.decimal.frac = 0;
  3105. v->value.decimal.total = ret;
  3106. *val = v;
  3107. }
  3108. }
  3109. goto return0;
  3110. }
  3111. case XML_SCHEMAS_UINT:
  3112. case XML_SCHEMAS_ULONG:
  3113. case XML_SCHEMAS_USHORT:
  3114. case XML_SCHEMAS_UBYTE:{
  3115. const xmlChar *cur = value;
  3116. unsigned long lo, mi, hi;
  3117. if (cur == NULL)
  3118. goto return1;
  3119. ret = xmlSchemaParseUInt(&cur, &lo, &mi, &hi);
  3120. if (ret < 0)
  3121. goto return1;
  3122. if (*cur != 0)
  3123. goto return1;
  3124. if (type->builtInType == XML_SCHEMAS_ULONG) {
  3125. if (hi >= 1844) {
  3126. if (hi > 1844)
  3127. goto return1;
  3128. if (mi >= 67440737) {
  3129. if (mi > 67440737)
  3130. goto return1;
  3131. if (lo > 9551615)
  3132. goto return1;
  3133. }
  3134. }
  3135. } else if (type->builtInType == XML_SCHEMAS_UINT) {
  3136. if (hi != 0)
  3137. goto return1;
  3138. if (mi >= 42) {
  3139. if (mi > 42)
  3140. goto return1;
  3141. if (lo > 94967295)
  3142. goto return1;
  3143. }
  3144. } else if (type->builtInType == XML_SCHEMAS_USHORT) {
  3145. if ((mi != 0) || (hi != 0))
  3146. goto return1;
  3147. if (lo > 65535)
  3148. goto return1;
  3149. } else if (type->builtInType == XML_SCHEMAS_UBYTE) {
  3150. if ((mi != 0) || (hi != 0))
  3151. goto return1;
  3152. if (lo > 255)
  3153. goto return1;
  3154. }
  3155. if (val != NULL) {
  3156. v = xmlSchemaNewValue(type->builtInType);
  3157. if (v != NULL) {
  3158. v->value.decimal.lo = lo;
  3159. v->value.decimal.mi = mi;
  3160. v->value.decimal.hi = hi;
  3161. v->value.decimal.sign = 0;
  3162. v->value.decimal.frac = 0;
  3163. v->value.decimal.total = ret;
  3164. *val = v;
  3165. }
  3166. }
  3167. goto return0;
  3168. }
  3169. }
  3170. done:
  3171. if (norm != NULL)
  3172. xmlFree(norm);
  3173. return (ret);
  3174. return3:
  3175. if (norm != NULL)
  3176. xmlFree(norm);
  3177. return (3);
  3178. return1:
  3179. if (norm != NULL)
  3180. xmlFree(norm);
  3181. return (1);
  3182. return0:
  3183. if (norm != NULL)
  3184. xmlFree(norm);
  3185. return (0);
  3186. error:
  3187. if (norm != NULL)
  3188. xmlFree(norm);
  3189. return (-1);
  3190. }
  3191. /**
  3192. * xmlSchemaValPredefTypeNode:
  3193. * @type: the predefined type
  3194. * @value: the value to check
  3195. * @val: the return computed value
  3196. * @node: the node containing the value
  3197. *
  3198. * Check that a value conforms to the lexical space of the predefined type.
  3199. * if true a value is computed and returned in @val.
  3200. *
  3201. * Returns 0 if this validates, a positive error code number otherwise
  3202. * and -1 in case of internal or API error.
  3203. */
  3204. int
  3205. xmlSchemaValPredefTypeNode(xmlSchemaTypePtr type, const xmlChar *value,
  3206. xmlSchemaValPtr *val, xmlNodePtr node) {
  3207. return(xmlSchemaValAtomicType(type, value, val, node, 0,
  3208. XML_SCHEMA_WHITESPACE_UNKNOWN, 1, 1, 0));
  3209. }
  3210. /**
  3211. * xmlSchemaValPredefTypeNodeNoNorm:
  3212. * @type: the predefined type
  3213. * @value: the value to check
  3214. * @val: the return computed value
  3215. * @node: the node containing the value
  3216. *
  3217. * Check that a value conforms to the lexical space of the predefined type.
  3218. * if true a value is computed and returned in @val.
  3219. * This one does apply any normalization to the value.
  3220. *
  3221. * Returns 0 if this validates, a positive error code number otherwise
  3222. * and -1 in case of internal or API error.
  3223. */
  3224. int
  3225. xmlSchemaValPredefTypeNodeNoNorm(xmlSchemaTypePtr type, const xmlChar *value,
  3226. xmlSchemaValPtr *val, xmlNodePtr node) {
  3227. return(xmlSchemaValAtomicType(type, value, val, node, 1,
  3228. XML_SCHEMA_WHITESPACE_UNKNOWN, 1, 0, 1));
  3229. }
  3230. /**
  3231. * xmlSchemaValidatePredefinedType:
  3232. * @type: the predefined type
  3233. * @value: the value to check
  3234. * @val: the return computed value
  3235. *
  3236. * Check that a value conforms to the lexical space of the predefined type.
  3237. * if true a value is computed and returned in @val.
  3238. *
  3239. * Returns 0 if this validates, a positive error code number otherwise
  3240. * and -1 in case of internal or API error.
  3241. */
  3242. int
  3243. xmlSchemaValidatePredefinedType(xmlSchemaTypePtr type, const xmlChar *value,
  3244. xmlSchemaValPtr *val) {
  3245. return(xmlSchemaValPredefTypeNode(type, value, val, NULL));
  3246. }
  3247. /**
  3248. * xmlSchemaCompareDecimals:
  3249. * @x: a first decimal value
  3250. * @y: a second decimal value
  3251. *
  3252. * Compare 2 decimals
  3253. *
  3254. * Returns -1 if x < y, 0 if x == y, 1 if x > y and -2 in case of error
  3255. */
  3256. static int
  3257. xmlSchemaCompareDecimals(xmlSchemaValPtr x, xmlSchemaValPtr y)
  3258. {
  3259. xmlSchemaValPtr swp;
  3260. int order = 1, integx, integy, dlen;
  3261. unsigned long hi, mi, lo;
  3262. /*
  3263. * First test: If x is -ve and not zero
  3264. */
  3265. if ((x->value.decimal.sign) &&
  3266. ((x->value.decimal.lo != 0) ||
  3267. (x->value.decimal.mi != 0) ||
  3268. (x->value.decimal.hi != 0))) {
  3269. /*
  3270. * Then if y is -ve and not zero reverse the compare
  3271. */
  3272. if ((y->value.decimal.sign) &&
  3273. ((y->value.decimal.lo != 0) ||
  3274. (y->value.decimal.mi != 0) ||
  3275. (y->value.decimal.hi != 0)))
  3276. order = -1;
  3277. /*
  3278. * Otherwise (y >= 0) we have the answer
  3279. */
  3280. else
  3281. return (-1);
  3282. /*
  3283. * If x is not -ve and y is -ve we have the answer
  3284. */
  3285. } else if ((y->value.decimal.sign) &&
  3286. ((y->value.decimal.lo != 0) ||
  3287. (y->value.decimal.mi != 0) ||
  3288. (y->value.decimal.hi != 0))) {
  3289. return (1);
  3290. }
  3291. /*
  3292. * If it's not simply determined by a difference in sign,
  3293. * then we need to compare the actual values of the two nums.
  3294. * To do this, we start by looking at the integral parts.
  3295. * If the number of integral digits differ, then we have our
  3296. * answer.
  3297. */
  3298. integx = x->value.decimal.total - x->value.decimal.frac;
  3299. integy = y->value.decimal.total - y->value.decimal.frac;
  3300. /*
  3301. * NOTE: We changed the "total" for values like "0.1"
  3302. * (or "-0.1" or ".1") to be 1, which was 2 previously.
  3303. * Therefore the special case, when such values are
  3304. * compared with 0, needs to be handled separately;
  3305. * otherwise a zero would be recognized incorrectly as
  3306. * greater than those values. This has the nice side effect
  3307. * that we gain an overall optimized comparison with zeroes.
  3308. * Note that a "0" has a "total" of 1 already.
  3309. */
  3310. if (integx == 1) {
  3311. if (x->value.decimal.lo == 0) {
  3312. if (integy != 1)
  3313. return -order;
  3314. else if (y->value.decimal.lo != 0)
  3315. return -order;
  3316. else
  3317. return(0);
  3318. }
  3319. }
  3320. if (integy == 1) {
  3321. if (y->value.decimal.lo == 0) {
  3322. if (integx != 1)
  3323. return order;
  3324. else if (x->value.decimal.lo != 0)
  3325. return order;
  3326. else
  3327. return(0);
  3328. }
  3329. }
  3330. if (integx > integy)
  3331. return order;
  3332. else if (integy > integx)
  3333. return -order;
  3334. /*
  3335. * If the number of integral digits is the same for both numbers,
  3336. * then things get a little more complicated. We need to "normalize"
  3337. * the numbers in order to properly compare them. To do this, we
  3338. * look at the total length of each number (length => number of
  3339. * significant digits), and divide the "shorter" by 10 (decreasing
  3340. * the length) until they are of equal length.
  3341. */
  3342. dlen = x->value.decimal.total - y->value.decimal.total;
  3343. if (dlen < 0) { /* y has more digits than x */
  3344. swp = x;
  3345. hi = y->value.decimal.hi;
  3346. mi = y->value.decimal.mi;
  3347. lo = y->value.decimal.lo;
  3348. dlen = -dlen;
  3349. order = -order;
  3350. } else { /* x has more digits than y */
  3351. swp = y;
  3352. hi = x->value.decimal.hi;
  3353. mi = x->value.decimal.mi;
  3354. lo = x->value.decimal.lo;
  3355. }
  3356. while (dlen > 8) { /* in effect, right shift by 10**8 */
  3357. lo = mi;
  3358. mi = hi;
  3359. hi = 0;
  3360. dlen -= 8;
  3361. }
  3362. while (dlen > 0) {
  3363. unsigned long rem1, rem2;
  3364. rem1 = (hi % 10) * 100000000L;
  3365. hi = hi / 10;
  3366. rem2 = (mi % 10) * 100000000L;
  3367. mi = (mi + rem1) / 10;
  3368. lo = (lo + rem2) / 10;
  3369. dlen--;
  3370. }
  3371. if (hi > swp->value.decimal.hi) {
  3372. return order;
  3373. } else if (hi == swp->value.decimal.hi) {
  3374. if (mi > swp->value.decimal.mi) {
  3375. return order;
  3376. } else if (mi == swp->value.decimal.mi) {
  3377. if (lo > swp->value.decimal.lo) {
  3378. return order;
  3379. } else if (lo == swp->value.decimal.lo) {
  3380. if (x->value.decimal.total == y->value.decimal.total) {
  3381. return 0;
  3382. } else {
  3383. return order;
  3384. }
  3385. }
  3386. }
  3387. }
  3388. return -order;
  3389. }
  3390. /**
  3391. * xmlSchemaCompareDurations:
  3392. * @x: a first duration value
  3393. * @y: a second duration value
  3394. *
  3395. * Compare 2 durations
  3396. *
  3397. * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
  3398. * case of error
  3399. */
  3400. static int
  3401. xmlSchemaCompareDurations(xmlSchemaValPtr x, xmlSchemaValPtr y)
  3402. {
  3403. long carry, mon, day;
  3404. double sec;
  3405. int invert = 1;
  3406. long xmon, xday, myear, minday, maxday;
  3407. static const long dayRange [2][12] = {
  3408. { 0, 28, 59, 89, 120, 150, 181, 212, 242, 273, 303, 334, },
  3409. { 0, 31, 62, 92, 123, 153, 184, 215, 245, 276, 306, 337} };
  3410. if ((x == NULL) || (y == NULL))
  3411. return -2;
  3412. /* months */
  3413. mon = x->value.dur.mon - y->value.dur.mon;
  3414. /* seconds */
  3415. sec = x->value.dur.sec - y->value.dur.sec;
  3416. carry = (long)(sec / SECS_PER_DAY);
  3417. sec -= ((double)carry) * SECS_PER_DAY;
  3418. /* days */
  3419. day = x->value.dur.day - y->value.dur.day + carry;
  3420. /* easy test */
  3421. if (mon == 0) {
  3422. if (day == 0)
  3423. if (sec == 0.0)
  3424. return 0;
  3425. else if (sec < 0.0)
  3426. return -1;
  3427. else
  3428. return 1;
  3429. else if (day < 0)
  3430. return -1;
  3431. else
  3432. return 1;
  3433. }
  3434. if (mon > 0) {
  3435. if ((day >= 0) && (sec >= 0.0))
  3436. return 1;
  3437. else {
  3438. xmon = mon;
  3439. xday = -day;
  3440. }
  3441. } else if ((day <= 0) && (sec <= 0.0)) {
  3442. return -1;
  3443. } else {
  3444. invert = -1;
  3445. xmon = -mon;
  3446. xday = day;
  3447. }
  3448. myear = xmon / 12;
  3449. if (myear == 0) {
  3450. minday = 0;
  3451. maxday = 0;
  3452. } else {
  3453. if (myear > LONG_MAX / 366)
  3454. return -2;
  3455. /* FIXME: This doesn't take leap year exceptions every 100/400 years
  3456. into account. */
  3457. maxday = 365 * myear + (myear + 3) / 4;
  3458. /* FIXME: Needs to be calculated separately */
  3459. minday = maxday - 1;
  3460. }
  3461. xmon = xmon % 12;
  3462. minday += dayRange[0][xmon];
  3463. maxday += dayRange[1][xmon];
  3464. if ((maxday == minday) && (maxday == xday))
  3465. return(0); /* can this really happen ? */
  3466. if (maxday < xday)
  3467. return(-invert);
  3468. if (minday > xday)
  3469. return(invert);
  3470. /* indeterminate */
  3471. return 2;
  3472. }
  3473. /*
  3474. * macros for adding date/times and durations
  3475. */
  3476. #define FQUOTIENT(a,b) (floor(((double)a/(double)b)))
  3477. #define MODULO(a,b) (a - FQUOTIENT(a,b) * b)
  3478. #define FQUOTIENT_RANGE(a,low,high) (FQUOTIENT((a-low),(high-low)))
  3479. #define MODULO_RANGE(a,low,high) ((MODULO((a-low),(high-low)))+low)
  3480. /**
  3481. * xmlSchemaDupVal:
  3482. * @v: the #xmlSchemaValPtr value to duplicate
  3483. *
  3484. * Makes a copy of @v. The calling program is responsible for freeing
  3485. * the returned value.
  3486. *
  3487. * returns a pointer to a duplicated #xmlSchemaValPtr or NULL if error.
  3488. */
  3489. static xmlSchemaValPtr
  3490. xmlSchemaDupVal (xmlSchemaValPtr v)
  3491. {
  3492. xmlSchemaValPtr ret = xmlSchemaNewValue(v->type);
  3493. if (ret == NULL)
  3494. return NULL;
  3495. memcpy(ret, v, sizeof(xmlSchemaVal));
  3496. ret->next = NULL;
  3497. return ret;
  3498. }
  3499. /**
  3500. * xmlSchemaCopyValue:
  3501. * @val: the precomputed value to be copied
  3502. *
  3503. * Copies the precomputed value. This duplicates any string within.
  3504. *
  3505. * Returns the copy or NULL if a copy for a data-type is not implemented.
  3506. */
  3507. xmlSchemaValPtr
  3508. xmlSchemaCopyValue(xmlSchemaValPtr val)
  3509. {
  3510. xmlSchemaValPtr ret = NULL, prev = NULL, cur;
  3511. /*
  3512. * Copy the string values.
  3513. */
  3514. while (val != NULL) {
  3515. switch (val->type) {
  3516. case XML_SCHEMAS_ANYTYPE:
  3517. case XML_SCHEMAS_IDREFS:
  3518. case XML_SCHEMAS_ENTITIES:
  3519. case XML_SCHEMAS_NMTOKENS:
  3520. xmlSchemaFreeValue(ret);
  3521. return (NULL);
  3522. case XML_SCHEMAS_ANYSIMPLETYPE:
  3523. case XML_SCHEMAS_STRING:
  3524. case XML_SCHEMAS_NORMSTRING:
  3525. case XML_SCHEMAS_TOKEN:
  3526. case XML_SCHEMAS_LANGUAGE:
  3527. case XML_SCHEMAS_NAME:
  3528. case XML_SCHEMAS_NCNAME:
  3529. case XML_SCHEMAS_ID:
  3530. case XML_SCHEMAS_IDREF:
  3531. case XML_SCHEMAS_ENTITY:
  3532. case XML_SCHEMAS_NMTOKEN:
  3533. case XML_SCHEMAS_ANYURI:
  3534. cur = xmlSchemaDupVal(val);
  3535. if (val->value.str != NULL)
  3536. cur->value.str = xmlStrdup(BAD_CAST val->value.str);
  3537. break;
  3538. case XML_SCHEMAS_QNAME:
  3539. case XML_SCHEMAS_NOTATION:
  3540. cur = xmlSchemaDupVal(val);
  3541. if (val->value.qname.name != NULL)
  3542. cur->value.qname.name =
  3543. xmlStrdup(BAD_CAST val->value.qname.name);
  3544. if (val->value.qname.uri != NULL)
  3545. cur->value.qname.uri =
  3546. xmlStrdup(BAD_CAST val->value.qname.uri);
  3547. break;
  3548. case XML_SCHEMAS_HEXBINARY:
  3549. cur = xmlSchemaDupVal(val);
  3550. if (val->value.hex.str != NULL)
  3551. cur->value.hex.str = xmlStrdup(BAD_CAST val->value.hex.str);
  3552. break;
  3553. case XML_SCHEMAS_BASE64BINARY:
  3554. cur = xmlSchemaDupVal(val);
  3555. if (val->value.base64.str != NULL)
  3556. cur->value.base64.str =
  3557. xmlStrdup(BAD_CAST val->value.base64.str);
  3558. break;
  3559. default:
  3560. cur = xmlSchemaDupVal(val);
  3561. break;
  3562. }
  3563. if (ret == NULL)
  3564. ret = cur;
  3565. else
  3566. prev->next = cur;
  3567. prev = cur;
  3568. val = val->next;
  3569. }
  3570. return (ret);
  3571. }
  3572. /**
  3573. * _xmlSchemaDateAdd:
  3574. * @dt: an #xmlSchemaValPtr
  3575. * @dur: an #xmlSchemaValPtr of type #XS_DURATION
  3576. *
  3577. * Compute a new date/time from @dt and @dur. This function assumes @dt
  3578. * is either #XML_SCHEMAS_DATETIME, #XML_SCHEMAS_DATE, #XML_SCHEMAS_GYEARMONTH,
  3579. * or #XML_SCHEMAS_GYEAR. The returned #xmlSchemaVal is the same type as
  3580. * @dt. The calling program is responsible for freeing the returned value.
  3581. *
  3582. * Returns a pointer to a new #xmlSchemaVal or NULL if error.
  3583. */
  3584. static xmlSchemaValPtr
  3585. _xmlSchemaDateAdd (xmlSchemaValPtr dt, xmlSchemaValPtr dur)
  3586. {
  3587. xmlSchemaValPtr ret, tmp;
  3588. long carry, tempdays, temp;
  3589. xmlSchemaValDatePtr r, d;
  3590. xmlSchemaValDurationPtr u;
  3591. if ((dt == NULL) || (dur == NULL))
  3592. return NULL;
  3593. ret = xmlSchemaNewValue(dt->type);
  3594. if (ret == NULL)
  3595. return NULL;
  3596. /* make a copy so we don't alter the original value */
  3597. tmp = xmlSchemaDupVal(dt);
  3598. if (tmp == NULL) {
  3599. xmlSchemaFreeValue(ret);
  3600. return NULL;
  3601. }
  3602. r = &(ret->value.date);
  3603. d = &(tmp->value.date);
  3604. u = &(dur->value.dur);
  3605. /* normalization */
  3606. if (d->mon == 0)
  3607. d->mon = 1;
  3608. /* normalize for time zone offset */
  3609. u->sec -= (d->tzo * 60);
  3610. d->tzo = 0;
  3611. /* normalization */
  3612. if (d->day == 0)
  3613. d->day = 1;
  3614. /* month */
  3615. carry = d->mon + u->mon;
  3616. r->mon = (unsigned int) MODULO_RANGE(carry, 1, 13);
  3617. carry = (long) FQUOTIENT_RANGE(carry, 1, 13);
  3618. /* year (may be modified later) */
  3619. r->year = d->year + carry;
  3620. if (r->year == 0) {
  3621. if (d->year > 0)
  3622. r->year--;
  3623. else
  3624. r->year++;
  3625. }
  3626. /* time zone */
  3627. r->tzo = d->tzo;
  3628. r->tz_flag = d->tz_flag;
  3629. /* seconds */
  3630. r->sec = d->sec + u->sec;
  3631. carry = (long) FQUOTIENT((long)r->sec, 60);
  3632. if (r->sec != 0.0) {
  3633. r->sec = MODULO(r->sec, 60.0);
  3634. }
  3635. /* minute */
  3636. carry += d->min;
  3637. r->min = (unsigned int) MODULO(carry, 60);
  3638. carry = (long) FQUOTIENT(carry, 60);
  3639. /* hours */
  3640. carry += d->hour;
  3641. r->hour = (unsigned int) MODULO(carry, 24);
  3642. carry = (long)FQUOTIENT(carry, 24);
  3643. /*
  3644. * days
  3645. * Note we use tempdays because the temporary values may need more
  3646. * than 5 bits
  3647. */
  3648. if ((VALID_YEAR(r->year)) && (VALID_MONTH(r->mon)) &&
  3649. (d->day > MAX_DAYINMONTH(r->year, r->mon)))
  3650. tempdays = MAX_DAYINMONTH(r->year, r->mon);
  3651. else if (d->day < 1)
  3652. tempdays = 1;
  3653. else
  3654. tempdays = d->day;
  3655. tempdays += u->day + carry;
  3656. while (1) {
  3657. if (tempdays < 1) {
  3658. long tmon = (long) MODULO_RANGE((int)r->mon-1, 1, 13);
  3659. long tyr = r->year + (long)FQUOTIENT_RANGE((int)r->mon-1, 1, 13);
  3660. if (tyr == 0)
  3661. tyr--;
  3662. /*
  3663. * Coverity detected an overrun in daysInMonth
  3664. * of size 12 at position 12 with index variable "((r)->mon - 1)"
  3665. */
  3666. if (tmon < 1)
  3667. tmon = 1;
  3668. if (tmon > 12)
  3669. tmon = 12;
  3670. tempdays += MAX_DAYINMONTH(tyr, tmon);
  3671. carry = -1;
  3672. } else if (VALID_YEAR(r->year) && VALID_MONTH(r->mon) &&
  3673. tempdays > (long) MAX_DAYINMONTH(r->year, r->mon)) {
  3674. tempdays = tempdays - MAX_DAYINMONTH(r->year, r->mon);
  3675. carry = 1;
  3676. } else
  3677. break;
  3678. temp = r->mon + carry;
  3679. r->mon = (unsigned int) MODULO_RANGE(temp, 1, 13);
  3680. r->year = r->year + (long) FQUOTIENT_RANGE(temp, 1, 13);
  3681. if (r->year == 0) {
  3682. if (temp < 1)
  3683. r->year--;
  3684. else
  3685. r->year++;
  3686. }
  3687. }
  3688. r->day = tempdays;
  3689. /*
  3690. * adjust the date/time type to the date values
  3691. */
  3692. if (ret->type != XML_SCHEMAS_DATETIME) {
  3693. if ((r->hour) || (r->min) || (r->sec))
  3694. ret->type = XML_SCHEMAS_DATETIME;
  3695. else if (ret->type != XML_SCHEMAS_DATE) {
  3696. if ((r->mon != 1) && (r->day != 1))
  3697. ret->type = XML_SCHEMAS_DATE;
  3698. else if ((ret->type != XML_SCHEMAS_GYEARMONTH) && (r->mon != 1))
  3699. ret->type = XML_SCHEMAS_GYEARMONTH;
  3700. }
  3701. }
  3702. xmlSchemaFreeValue(tmp);
  3703. return ret;
  3704. }
  3705. /**
  3706. * xmlSchemaDateNormalize:
  3707. * @dt: an #xmlSchemaValPtr of a date/time type value.
  3708. * @offset: number of seconds to adjust @dt by.
  3709. *
  3710. * Normalize @dt to GMT time. The @offset parameter is subtracted from
  3711. * the return value is a time-zone offset is present on @dt.
  3712. *
  3713. * Returns a normalized copy of @dt or NULL if error.
  3714. */
  3715. static xmlSchemaValPtr
  3716. xmlSchemaDateNormalize (xmlSchemaValPtr dt, double offset)
  3717. {
  3718. xmlSchemaValPtr dur, ret;
  3719. if (dt == NULL)
  3720. return NULL;
  3721. if (((dt->type != XML_SCHEMAS_TIME) &&
  3722. (dt->type != XML_SCHEMAS_DATETIME) &&
  3723. (dt->type != XML_SCHEMAS_DATE)) || (dt->value.date.tzo == 0))
  3724. return xmlSchemaDupVal(dt);
  3725. dur = xmlSchemaNewValue(XML_SCHEMAS_DURATION);
  3726. if (dur == NULL)
  3727. return NULL;
  3728. dur->value.date.sec -= offset;
  3729. ret = _xmlSchemaDateAdd(dt, dur);
  3730. if (ret == NULL)
  3731. return NULL;
  3732. xmlSchemaFreeValue(dur);
  3733. /* ret->value.date.tzo = 0; */
  3734. return ret;
  3735. }
  3736. /**
  3737. * _xmlSchemaDateCastYMToDays:
  3738. * @dt: an #xmlSchemaValPtr
  3739. *
  3740. * Convert mon and year of @dt to total number of days. Take the
  3741. * number of years since (or before) 1 AD and add the number of leap
  3742. * years. This is a function because negative
  3743. * years must be handled a little differently and there is no zero year.
  3744. *
  3745. * Returns number of days.
  3746. */
  3747. static long
  3748. _xmlSchemaDateCastYMToDays (const xmlSchemaValPtr dt)
  3749. {
  3750. long ret;
  3751. int mon;
  3752. mon = dt->value.date.mon;
  3753. if (mon <= 0) mon = 1; /* normalization */
  3754. if (dt->value.date.year <= 0)
  3755. ret = (dt->value.date.year * 365) +
  3756. (((dt->value.date.year+1)/4)-((dt->value.date.year+1)/100)+
  3757. ((dt->value.date.year+1)/400)) +
  3758. DAY_IN_YEAR(0, mon, dt->value.date.year);
  3759. else
  3760. ret = ((dt->value.date.year-1) * 365) +
  3761. (((dt->value.date.year-1)/4)-((dt->value.date.year-1)/100)+
  3762. ((dt->value.date.year-1)/400)) +
  3763. DAY_IN_YEAR(0, mon, dt->value.date.year);
  3764. return ret;
  3765. }
  3766. /**
  3767. * TIME_TO_NUMBER:
  3768. * @dt: an #xmlSchemaValPtr
  3769. *
  3770. * Calculates the number of seconds in the time portion of @dt.
  3771. *
  3772. * Returns seconds.
  3773. */
  3774. #define TIME_TO_NUMBER(dt) \
  3775. ((double)((dt->value.date.hour * SECS_PER_HOUR) + \
  3776. (dt->value.date.min * SECS_PER_MIN) + \
  3777. (dt->value.date.tzo * SECS_PER_MIN)) + \
  3778. dt->value.date.sec)
  3779. /**
  3780. * xmlSchemaCompareDates:
  3781. * @x: a first date/time value
  3782. * @y: a second date/time value
  3783. *
  3784. * Compare 2 date/times
  3785. *
  3786. * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
  3787. * case of error
  3788. */
  3789. static int
  3790. xmlSchemaCompareDates (xmlSchemaValPtr x, xmlSchemaValPtr y)
  3791. {
  3792. unsigned char xmask, ymask, xor_mask, and_mask;
  3793. xmlSchemaValPtr p1, p2, q1, q2;
  3794. long p1d, p2d, q1d, q2d;
  3795. if ((x == NULL) || (y == NULL))
  3796. return -2;
  3797. if ((x->value.date.year > LONG_MAX / 366) ||
  3798. (x->value.date.year < LONG_MIN / 366) ||
  3799. (y->value.date.year > LONG_MAX / 366) ||
  3800. (y->value.date.year < LONG_MIN / 366)) {
  3801. /* Possible overflow when converting to days. */
  3802. return -2;
  3803. }
  3804. if (x->value.date.tz_flag) {
  3805. if (!y->value.date.tz_flag) {
  3806. p1 = xmlSchemaDateNormalize(x, 0);
  3807. p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day;
  3808. /* normalize y + 14:00 */
  3809. q1 = xmlSchemaDateNormalize(y, (14 * SECS_PER_HOUR));
  3810. q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day;
  3811. if (p1d < q1d) {
  3812. xmlSchemaFreeValue(p1);
  3813. xmlSchemaFreeValue(q1);
  3814. return -1;
  3815. } else if (p1d == q1d) {
  3816. double sec;
  3817. sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1);
  3818. if (sec < 0.0) {
  3819. xmlSchemaFreeValue(p1);
  3820. xmlSchemaFreeValue(q1);
  3821. return -1;
  3822. } else {
  3823. int ret = 0;
  3824. /* normalize y - 14:00 */
  3825. q2 = xmlSchemaDateNormalize(y, -(14 * SECS_PER_HOUR));
  3826. q2d = _xmlSchemaDateCastYMToDays(q2) + q2->value.date.day;
  3827. if (p1d > q2d)
  3828. ret = 1;
  3829. else if (p1d == q2d) {
  3830. sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q2);
  3831. if (sec > 0.0)
  3832. ret = 1;
  3833. else
  3834. ret = 2; /* indeterminate */
  3835. }
  3836. xmlSchemaFreeValue(p1);
  3837. xmlSchemaFreeValue(q1);
  3838. xmlSchemaFreeValue(q2);
  3839. if (ret != 0)
  3840. return(ret);
  3841. }
  3842. } else {
  3843. xmlSchemaFreeValue(p1);
  3844. xmlSchemaFreeValue(q1);
  3845. }
  3846. }
  3847. } else if (y->value.date.tz_flag) {
  3848. q1 = xmlSchemaDateNormalize(y, 0);
  3849. q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day;
  3850. /* normalize x - 14:00 */
  3851. p1 = xmlSchemaDateNormalize(x, -(14 * SECS_PER_HOUR));
  3852. p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day;
  3853. if (p1d < q1d) {
  3854. xmlSchemaFreeValue(p1);
  3855. xmlSchemaFreeValue(q1);
  3856. return -1;
  3857. } else if (p1d == q1d) {
  3858. double sec;
  3859. sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1);
  3860. if (sec < 0.0) {
  3861. xmlSchemaFreeValue(p1);
  3862. xmlSchemaFreeValue(q1);
  3863. return -1;
  3864. } else {
  3865. int ret = 0;
  3866. /* normalize x + 14:00 */
  3867. p2 = xmlSchemaDateNormalize(x, (14 * SECS_PER_HOUR));
  3868. p2d = _xmlSchemaDateCastYMToDays(p2) + p2->value.date.day;
  3869. if (p2d > q1d) {
  3870. ret = 1;
  3871. } else if (p2d == q1d) {
  3872. sec = TIME_TO_NUMBER(p2) - TIME_TO_NUMBER(q1);
  3873. if (sec > 0.0)
  3874. ret = 1;
  3875. else
  3876. ret = 2; /* indeterminate */
  3877. }
  3878. xmlSchemaFreeValue(p1);
  3879. xmlSchemaFreeValue(q1);
  3880. xmlSchemaFreeValue(p2);
  3881. if (ret != 0)
  3882. return(ret);
  3883. }
  3884. } else {
  3885. xmlSchemaFreeValue(p1);
  3886. xmlSchemaFreeValue(q1);
  3887. }
  3888. }
  3889. /*
  3890. * if the same type then calculate the difference
  3891. */
  3892. if (x->type == y->type) {
  3893. int ret = 0;
  3894. q1 = xmlSchemaDateNormalize(y, 0);
  3895. q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day;
  3896. p1 = xmlSchemaDateNormalize(x, 0);
  3897. p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day;
  3898. if (p1d < q1d) {
  3899. ret = -1;
  3900. } else if (p1d > q1d) {
  3901. ret = 1;
  3902. } else {
  3903. double sec;
  3904. sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1);
  3905. if (sec < 0.0)
  3906. ret = -1;
  3907. else if (sec > 0.0)
  3908. ret = 1;
  3909. }
  3910. xmlSchemaFreeValue(p1);
  3911. xmlSchemaFreeValue(q1);
  3912. return(ret);
  3913. }
  3914. switch (x->type) {
  3915. case XML_SCHEMAS_DATETIME:
  3916. xmask = 0xf;
  3917. break;
  3918. case XML_SCHEMAS_DATE:
  3919. xmask = 0x7;
  3920. break;
  3921. case XML_SCHEMAS_GYEAR:
  3922. xmask = 0x1;
  3923. break;
  3924. case XML_SCHEMAS_GMONTH:
  3925. xmask = 0x2;
  3926. break;
  3927. case XML_SCHEMAS_GDAY:
  3928. xmask = 0x3;
  3929. break;
  3930. case XML_SCHEMAS_GYEARMONTH:
  3931. xmask = 0x3;
  3932. break;
  3933. case XML_SCHEMAS_GMONTHDAY:
  3934. xmask = 0x6;
  3935. break;
  3936. case XML_SCHEMAS_TIME:
  3937. xmask = 0x8;
  3938. break;
  3939. default:
  3940. xmask = 0;
  3941. break;
  3942. }
  3943. switch (y->type) {
  3944. case XML_SCHEMAS_DATETIME:
  3945. ymask = 0xf;
  3946. break;
  3947. case XML_SCHEMAS_DATE:
  3948. ymask = 0x7;
  3949. break;
  3950. case XML_SCHEMAS_GYEAR:
  3951. ymask = 0x1;
  3952. break;
  3953. case XML_SCHEMAS_GMONTH:
  3954. ymask = 0x2;
  3955. break;
  3956. case XML_SCHEMAS_GDAY:
  3957. ymask = 0x3;
  3958. break;
  3959. case XML_SCHEMAS_GYEARMONTH:
  3960. ymask = 0x3;
  3961. break;
  3962. case XML_SCHEMAS_GMONTHDAY:
  3963. ymask = 0x6;
  3964. break;
  3965. case XML_SCHEMAS_TIME:
  3966. ymask = 0x8;
  3967. break;
  3968. default:
  3969. ymask = 0;
  3970. break;
  3971. }
  3972. xor_mask = xmask ^ ymask; /* mark type differences */
  3973. and_mask = xmask & ymask; /* mark field specification */
  3974. /* year */
  3975. if (xor_mask & 1)
  3976. return 2; /* indeterminate */
  3977. else if (and_mask & 1) {
  3978. if (x->value.date.year < y->value.date.year)
  3979. return -1;
  3980. else if (x->value.date.year > y->value.date.year)
  3981. return 1;
  3982. }
  3983. /* month */
  3984. if (xor_mask & 2)
  3985. return 2; /* indeterminate */
  3986. else if (and_mask & 2) {
  3987. if (x->value.date.mon < y->value.date.mon)
  3988. return -1;
  3989. else if (x->value.date.mon > y->value.date.mon)
  3990. return 1;
  3991. }
  3992. /* day */
  3993. if (xor_mask & 4)
  3994. return 2; /* indeterminate */
  3995. else if (and_mask & 4) {
  3996. if (x->value.date.day < y->value.date.day)
  3997. return -1;
  3998. else if (x->value.date.day > y->value.date.day)
  3999. return 1;
  4000. }
  4001. /* time */
  4002. if (xor_mask & 8)
  4003. return 2; /* indeterminate */
  4004. else if (and_mask & 8) {
  4005. if (x->value.date.hour < y->value.date.hour)
  4006. return -1;
  4007. else if (x->value.date.hour > y->value.date.hour)
  4008. return 1;
  4009. else if (x->value.date.min < y->value.date.min)
  4010. return -1;
  4011. else if (x->value.date.min > y->value.date.min)
  4012. return 1;
  4013. else if (x->value.date.sec < y->value.date.sec)
  4014. return -1;
  4015. else if (x->value.date.sec > y->value.date.sec)
  4016. return 1;
  4017. }
  4018. return 0;
  4019. }
  4020. /**
  4021. * xmlSchemaComparePreserveReplaceStrings:
  4022. * @x: a first string value
  4023. * @y: a second string value
  4024. * @invert: inverts the result if x < y or x > y.
  4025. *
  4026. * Compare 2 string for their normalized values.
  4027. * @x is a string with whitespace of "preserve", @y is
  4028. * a string with a whitespace of "replace". I.e. @x could
  4029. * be an "xsd:string" and @y an "xsd:normalizedString".
  4030. *
  4031. * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
  4032. * case of error
  4033. */
  4034. static int
  4035. xmlSchemaComparePreserveReplaceStrings(const xmlChar *x,
  4036. const xmlChar *y,
  4037. int invert)
  4038. {
  4039. int tmp;
  4040. while ((*x != 0) && (*y != 0)) {
  4041. if (IS_WSP_REPLACE_CH(*y)) {
  4042. if (! IS_WSP_SPACE_CH(*x)) {
  4043. if ((*x - 0x20) < 0) {
  4044. if (invert)
  4045. return(1);
  4046. else
  4047. return(-1);
  4048. } else {
  4049. if (invert)
  4050. return(-1);
  4051. else
  4052. return(1);
  4053. }
  4054. }
  4055. } else {
  4056. tmp = *x - *y;
  4057. if (tmp < 0) {
  4058. if (invert)
  4059. return(1);
  4060. else
  4061. return(-1);
  4062. }
  4063. if (tmp > 0) {
  4064. if (invert)
  4065. return(-1);
  4066. else
  4067. return(1);
  4068. }
  4069. }
  4070. x++;
  4071. y++;
  4072. }
  4073. if (*x != 0) {
  4074. if (invert)
  4075. return(-1);
  4076. else
  4077. return(1);
  4078. }
  4079. if (*y != 0) {
  4080. if (invert)
  4081. return(1);
  4082. else
  4083. return(-1);
  4084. }
  4085. return(0);
  4086. }
  4087. /**
  4088. * xmlSchemaComparePreserveCollapseStrings:
  4089. * @x: a first string value
  4090. * @y: a second string value
  4091. *
  4092. * Compare 2 string for their normalized values.
  4093. * @x is a string with whitespace of "preserve", @y is
  4094. * a string with a whitespace of "collapse". I.e. @x could
  4095. * be an "xsd:string" and @y an "xsd:normalizedString".
  4096. *
  4097. * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
  4098. * case of error
  4099. */
  4100. static int
  4101. xmlSchemaComparePreserveCollapseStrings(const xmlChar *x,
  4102. const xmlChar *y,
  4103. int invert)
  4104. {
  4105. int tmp;
  4106. /*
  4107. * Skip leading blank chars of the collapsed string.
  4108. */
  4109. while IS_WSP_BLANK_CH(*y)
  4110. y++;
  4111. while ((*x != 0) && (*y != 0)) {
  4112. if IS_WSP_BLANK_CH(*y) {
  4113. if (! IS_WSP_SPACE_CH(*x)) {
  4114. /*
  4115. * The yv character would have been replaced to 0x20.
  4116. */
  4117. if ((*x - 0x20) < 0) {
  4118. if (invert)
  4119. return(1);
  4120. else
  4121. return(-1);
  4122. } else {
  4123. if (invert)
  4124. return(-1);
  4125. else
  4126. return(1);
  4127. }
  4128. }
  4129. x++;
  4130. y++;
  4131. /*
  4132. * Skip contiguous blank chars of the collapsed string.
  4133. */
  4134. while IS_WSP_BLANK_CH(*y)
  4135. y++;
  4136. } else {
  4137. tmp = *x++ - *y++;
  4138. if (tmp < 0) {
  4139. if (invert)
  4140. return(1);
  4141. else
  4142. return(-1);
  4143. }
  4144. if (tmp > 0) {
  4145. if (invert)
  4146. return(-1);
  4147. else
  4148. return(1);
  4149. }
  4150. }
  4151. }
  4152. if (*x != 0) {
  4153. if (invert)
  4154. return(-1);
  4155. else
  4156. return(1);
  4157. }
  4158. if (*y != 0) {
  4159. /*
  4160. * Skip trailing blank chars of the collapsed string.
  4161. */
  4162. while IS_WSP_BLANK_CH(*y)
  4163. y++;
  4164. if (*y != 0) {
  4165. if (invert)
  4166. return(1);
  4167. else
  4168. return(-1);
  4169. }
  4170. }
  4171. return(0);
  4172. }
  4173. /**
  4174. * xmlSchemaComparePreserveCollapseStrings:
  4175. * @x: a first string value
  4176. * @y: a second string value
  4177. *
  4178. * Compare 2 string for their normalized values.
  4179. * @x is a string with whitespace of "preserve", @y is
  4180. * a string with a whitespace of "collapse". I.e. @x could
  4181. * be an "xsd:string" and @y an "xsd:normalizedString".
  4182. *
  4183. * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
  4184. * case of error
  4185. */
  4186. static int
  4187. xmlSchemaCompareReplaceCollapseStrings(const xmlChar *x,
  4188. const xmlChar *y,
  4189. int invert)
  4190. {
  4191. int tmp;
  4192. /*
  4193. * Skip leading blank chars of the collapsed string.
  4194. */
  4195. while IS_WSP_BLANK_CH(*y)
  4196. y++;
  4197. while ((*x != 0) && (*y != 0)) {
  4198. if IS_WSP_BLANK_CH(*y) {
  4199. if (! IS_WSP_BLANK_CH(*x)) {
  4200. /*
  4201. * The yv character would have been replaced to 0x20.
  4202. */
  4203. if ((*x - 0x20) < 0) {
  4204. if (invert)
  4205. return(1);
  4206. else
  4207. return(-1);
  4208. } else {
  4209. if (invert)
  4210. return(-1);
  4211. else
  4212. return(1);
  4213. }
  4214. }
  4215. x++;
  4216. y++;
  4217. /*
  4218. * Skip contiguous blank chars of the collapsed string.
  4219. */
  4220. while IS_WSP_BLANK_CH(*y)
  4221. y++;
  4222. } else {
  4223. if IS_WSP_BLANK_CH(*x) {
  4224. /*
  4225. * The xv character would have been replaced to 0x20.
  4226. */
  4227. if ((0x20 - *y) < 0) {
  4228. if (invert)
  4229. return(1);
  4230. else
  4231. return(-1);
  4232. } else {
  4233. if (invert)
  4234. return(-1);
  4235. else
  4236. return(1);
  4237. }
  4238. }
  4239. tmp = *x++ - *y++;
  4240. if (tmp < 0)
  4241. return(-1);
  4242. if (tmp > 0)
  4243. return(1);
  4244. }
  4245. }
  4246. if (*x != 0) {
  4247. if (invert)
  4248. return(-1);
  4249. else
  4250. return(1);
  4251. }
  4252. if (*y != 0) {
  4253. /*
  4254. * Skip trailing blank chars of the collapsed string.
  4255. */
  4256. while IS_WSP_BLANK_CH(*y)
  4257. y++;
  4258. if (*y != 0) {
  4259. if (invert)
  4260. return(1);
  4261. else
  4262. return(-1);
  4263. }
  4264. }
  4265. return(0);
  4266. }
  4267. /**
  4268. * xmlSchemaCompareReplacedStrings:
  4269. * @x: a first string value
  4270. * @y: a second string value
  4271. *
  4272. * Compare 2 string for their normalized values.
  4273. *
  4274. * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
  4275. * case of error
  4276. */
  4277. static int
  4278. xmlSchemaCompareReplacedStrings(const xmlChar *x,
  4279. const xmlChar *y)
  4280. {
  4281. int tmp;
  4282. while ((*x != 0) && (*y != 0)) {
  4283. if IS_WSP_BLANK_CH(*y) {
  4284. if (! IS_WSP_BLANK_CH(*x)) {
  4285. if ((*x - 0x20) < 0)
  4286. return(-1);
  4287. else
  4288. return(1);
  4289. }
  4290. } else {
  4291. if IS_WSP_BLANK_CH(*x) {
  4292. if ((0x20 - *y) < 0)
  4293. return(-1);
  4294. else
  4295. return(1);
  4296. }
  4297. tmp = *x - *y;
  4298. if (tmp < 0)
  4299. return(-1);
  4300. if (tmp > 0)
  4301. return(1);
  4302. }
  4303. x++;
  4304. y++;
  4305. }
  4306. if (*x != 0)
  4307. return(1);
  4308. if (*y != 0)
  4309. return(-1);
  4310. return(0);
  4311. }
  4312. /**
  4313. * xmlSchemaCompareNormStrings:
  4314. * @x: a first string value
  4315. * @y: a second string value
  4316. *
  4317. * Compare 2 string for their normalized values.
  4318. *
  4319. * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
  4320. * case of error
  4321. */
  4322. static int
  4323. xmlSchemaCompareNormStrings(const xmlChar *x,
  4324. const xmlChar *y) {
  4325. int tmp;
  4326. while (IS_BLANK_CH(*x)) x++;
  4327. while (IS_BLANK_CH(*y)) y++;
  4328. while ((*x != 0) && (*y != 0)) {
  4329. if (IS_BLANK_CH(*x)) {
  4330. if (!IS_BLANK_CH(*y)) {
  4331. tmp = *x - *y;
  4332. return(tmp);
  4333. }
  4334. while (IS_BLANK_CH(*x)) x++;
  4335. while (IS_BLANK_CH(*y)) y++;
  4336. } else {
  4337. tmp = *x++ - *y++;
  4338. if (tmp < 0)
  4339. return(-1);
  4340. if (tmp > 0)
  4341. return(1);
  4342. }
  4343. }
  4344. if (*x != 0) {
  4345. while (IS_BLANK_CH(*x)) x++;
  4346. if (*x != 0)
  4347. return(1);
  4348. }
  4349. if (*y != 0) {
  4350. while (IS_BLANK_CH(*y)) y++;
  4351. if (*y != 0)
  4352. return(-1);
  4353. }
  4354. return(0);
  4355. }
  4356. /**
  4357. * xmlSchemaCompareFloats:
  4358. * @x: a first float or double value
  4359. * @y: a second float or double value
  4360. *
  4361. * Compare 2 values
  4362. *
  4363. * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
  4364. * case of error
  4365. */
  4366. static int
  4367. xmlSchemaCompareFloats(xmlSchemaValPtr x, xmlSchemaValPtr y) {
  4368. double d1, d2;
  4369. if ((x == NULL) || (y == NULL))
  4370. return(-2);
  4371. /*
  4372. * Cast everything to doubles.
  4373. */
  4374. if (x->type == XML_SCHEMAS_DOUBLE)
  4375. d1 = x->value.d;
  4376. else if (x->type == XML_SCHEMAS_FLOAT)
  4377. d1 = x->value.f;
  4378. else
  4379. return(-2);
  4380. if (y->type == XML_SCHEMAS_DOUBLE)
  4381. d2 = y->value.d;
  4382. else if (y->type == XML_SCHEMAS_FLOAT)
  4383. d2 = y->value.f;
  4384. else
  4385. return(-2);
  4386. /*
  4387. * Check for special cases.
  4388. */
  4389. if (xmlXPathIsNaN(d1)) {
  4390. if (xmlXPathIsNaN(d2))
  4391. return(0);
  4392. return(1);
  4393. }
  4394. if (xmlXPathIsNaN(d2))
  4395. return(-1);
  4396. if (d1 == xmlXPathPINF) {
  4397. if (d2 == xmlXPathPINF)
  4398. return(0);
  4399. return(1);
  4400. }
  4401. if (d2 == xmlXPathPINF)
  4402. return(-1);
  4403. if (d1 == xmlXPathNINF) {
  4404. if (d2 == xmlXPathNINF)
  4405. return(0);
  4406. return(-1);
  4407. }
  4408. if (d2 == xmlXPathNINF)
  4409. return(1);
  4410. /*
  4411. * basic tests, the last one we should have equality, but
  4412. * portability is more important than speed and handling
  4413. * NaN or Inf in a portable way is always a challenge, so ...
  4414. */
  4415. if (d1 < d2)
  4416. return(-1);
  4417. if (d1 > d2)
  4418. return(1);
  4419. if (d1 == d2)
  4420. return(0);
  4421. return(2);
  4422. }
  4423. /**
  4424. * xmlSchemaCompareValues:
  4425. * @x: a first value
  4426. * @xvalue: the first value as a string (optional)
  4427. * @xwtsp: the whitespace type
  4428. * @y: a second value
  4429. * @xvalue: the second value as a string (optional)
  4430. * @ywtsp: the whitespace type
  4431. *
  4432. * Compare 2 values
  4433. *
  4434. * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, 3 if not
  4435. * comparable and -2 in case of error
  4436. */
  4437. static int
  4438. xmlSchemaCompareValuesInternal(xmlSchemaValType xtype,
  4439. xmlSchemaValPtr x,
  4440. const xmlChar *xvalue,
  4441. xmlSchemaWhitespaceValueType xws,
  4442. xmlSchemaValType ytype,
  4443. xmlSchemaValPtr y,
  4444. const xmlChar *yvalue,
  4445. xmlSchemaWhitespaceValueType yws)
  4446. {
  4447. switch (xtype) {
  4448. case XML_SCHEMAS_UNKNOWN:
  4449. case XML_SCHEMAS_ANYTYPE:
  4450. return(-2);
  4451. case XML_SCHEMAS_INTEGER:
  4452. case XML_SCHEMAS_NPINTEGER:
  4453. case XML_SCHEMAS_NINTEGER:
  4454. case XML_SCHEMAS_NNINTEGER:
  4455. case XML_SCHEMAS_PINTEGER:
  4456. case XML_SCHEMAS_INT:
  4457. case XML_SCHEMAS_UINT:
  4458. case XML_SCHEMAS_LONG:
  4459. case XML_SCHEMAS_ULONG:
  4460. case XML_SCHEMAS_SHORT:
  4461. case XML_SCHEMAS_USHORT:
  4462. case XML_SCHEMAS_BYTE:
  4463. case XML_SCHEMAS_UBYTE:
  4464. case XML_SCHEMAS_DECIMAL:
  4465. if ((x == NULL) || (y == NULL))
  4466. return(-2);
  4467. if (ytype == xtype)
  4468. return(xmlSchemaCompareDecimals(x, y));
  4469. if ((ytype == XML_SCHEMAS_DECIMAL) ||
  4470. (ytype == XML_SCHEMAS_INTEGER) ||
  4471. (ytype == XML_SCHEMAS_NPINTEGER) ||
  4472. (ytype == XML_SCHEMAS_NINTEGER) ||
  4473. (ytype == XML_SCHEMAS_NNINTEGER) ||
  4474. (ytype == XML_SCHEMAS_PINTEGER) ||
  4475. (ytype == XML_SCHEMAS_INT) ||
  4476. (ytype == XML_SCHEMAS_UINT) ||
  4477. (ytype == XML_SCHEMAS_LONG) ||
  4478. (ytype == XML_SCHEMAS_ULONG) ||
  4479. (ytype == XML_SCHEMAS_SHORT) ||
  4480. (ytype == XML_SCHEMAS_USHORT) ||
  4481. (ytype == XML_SCHEMAS_BYTE) ||
  4482. (ytype == XML_SCHEMAS_UBYTE))
  4483. return(xmlSchemaCompareDecimals(x, y));
  4484. return(-2);
  4485. case XML_SCHEMAS_DURATION:
  4486. if ((x == NULL) || (y == NULL))
  4487. return(-2);
  4488. if (ytype == XML_SCHEMAS_DURATION)
  4489. return(xmlSchemaCompareDurations(x, y));
  4490. return(-2);
  4491. case XML_SCHEMAS_TIME:
  4492. case XML_SCHEMAS_GDAY:
  4493. case XML_SCHEMAS_GMONTH:
  4494. case XML_SCHEMAS_GMONTHDAY:
  4495. case XML_SCHEMAS_GYEAR:
  4496. case XML_SCHEMAS_GYEARMONTH:
  4497. case XML_SCHEMAS_DATE:
  4498. case XML_SCHEMAS_DATETIME:
  4499. if ((x == NULL) || (y == NULL))
  4500. return(-2);
  4501. if ((ytype == XML_SCHEMAS_DATETIME) ||
  4502. (ytype == XML_SCHEMAS_TIME) ||
  4503. (ytype == XML_SCHEMAS_GDAY) ||
  4504. (ytype == XML_SCHEMAS_GMONTH) ||
  4505. (ytype == XML_SCHEMAS_GMONTHDAY) ||
  4506. (ytype == XML_SCHEMAS_GYEAR) ||
  4507. (ytype == XML_SCHEMAS_DATE) ||
  4508. (ytype == XML_SCHEMAS_GYEARMONTH))
  4509. return (xmlSchemaCompareDates(x, y));
  4510. return (-2);
  4511. /*
  4512. * Note that we will support comparison of string types against
  4513. * anySimpleType as well.
  4514. */
  4515. case XML_SCHEMAS_ANYSIMPLETYPE:
  4516. case XML_SCHEMAS_STRING:
  4517. case XML_SCHEMAS_NORMSTRING:
  4518. case XML_SCHEMAS_TOKEN:
  4519. case XML_SCHEMAS_LANGUAGE:
  4520. case XML_SCHEMAS_NMTOKEN:
  4521. case XML_SCHEMAS_NAME:
  4522. case XML_SCHEMAS_NCNAME:
  4523. case XML_SCHEMAS_ID:
  4524. case XML_SCHEMAS_IDREF:
  4525. case XML_SCHEMAS_ENTITY:
  4526. case XML_SCHEMAS_ANYURI:
  4527. {
  4528. const xmlChar *xv, *yv;
  4529. if (x == NULL)
  4530. xv = xvalue;
  4531. else
  4532. xv = x->value.str;
  4533. if (y == NULL)
  4534. yv = yvalue;
  4535. else
  4536. yv = y->value.str;
  4537. /*
  4538. * TODO: Compare those against QName.
  4539. */
  4540. if (ytype == XML_SCHEMAS_QNAME) {
  4541. TODO
  4542. if (y == NULL)
  4543. return(-2);
  4544. return (-2);
  4545. }
  4546. if ((ytype == XML_SCHEMAS_ANYSIMPLETYPE) ||
  4547. (ytype == XML_SCHEMAS_STRING) ||
  4548. (ytype == XML_SCHEMAS_NORMSTRING) ||
  4549. (ytype == XML_SCHEMAS_TOKEN) ||
  4550. (ytype == XML_SCHEMAS_LANGUAGE) ||
  4551. (ytype == XML_SCHEMAS_NMTOKEN) ||
  4552. (ytype == XML_SCHEMAS_NAME) ||
  4553. (ytype == XML_SCHEMAS_NCNAME) ||
  4554. (ytype == XML_SCHEMAS_ID) ||
  4555. (ytype == XML_SCHEMAS_IDREF) ||
  4556. (ytype == XML_SCHEMAS_ENTITY) ||
  4557. (ytype == XML_SCHEMAS_ANYURI)) {
  4558. if (xws == XML_SCHEMA_WHITESPACE_PRESERVE) {
  4559. if (yws == XML_SCHEMA_WHITESPACE_PRESERVE) {
  4560. /* TODO: What about x < y or x > y. */
  4561. if (xmlStrEqual(xv, yv))
  4562. return (0);
  4563. else
  4564. return (2);
  4565. } else if (yws == XML_SCHEMA_WHITESPACE_REPLACE)
  4566. return (xmlSchemaComparePreserveReplaceStrings(xv, yv, 0));
  4567. else if (yws == XML_SCHEMA_WHITESPACE_COLLAPSE)
  4568. return (xmlSchemaComparePreserveCollapseStrings(xv, yv, 0));
  4569. } else if (xws == XML_SCHEMA_WHITESPACE_REPLACE) {
  4570. if (yws == XML_SCHEMA_WHITESPACE_PRESERVE)
  4571. return (xmlSchemaComparePreserveReplaceStrings(yv, xv, 1));
  4572. if (yws == XML_SCHEMA_WHITESPACE_REPLACE)
  4573. return (xmlSchemaCompareReplacedStrings(xv, yv));
  4574. if (yws == XML_SCHEMA_WHITESPACE_COLLAPSE)
  4575. return (xmlSchemaCompareReplaceCollapseStrings(xv, yv, 0));
  4576. } else if (xws == XML_SCHEMA_WHITESPACE_COLLAPSE) {
  4577. if (yws == XML_SCHEMA_WHITESPACE_PRESERVE)
  4578. return (xmlSchemaComparePreserveCollapseStrings(yv, xv, 1));
  4579. if (yws == XML_SCHEMA_WHITESPACE_REPLACE)
  4580. return (xmlSchemaCompareReplaceCollapseStrings(yv, xv, 1));
  4581. if (yws == XML_SCHEMA_WHITESPACE_COLLAPSE)
  4582. return (xmlSchemaCompareNormStrings(xv, yv));
  4583. } else
  4584. return (-2);
  4585. }
  4586. return (-2);
  4587. }
  4588. case XML_SCHEMAS_QNAME:
  4589. case XML_SCHEMAS_NOTATION:
  4590. if ((x == NULL) || (y == NULL))
  4591. return(-2);
  4592. if ((ytype == XML_SCHEMAS_QNAME) ||
  4593. (ytype == XML_SCHEMAS_NOTATION)) {
  4594. if ((xmlStrEqual(x->value.qname.name, y->value.qname.name)) &&
  4595. (xmlStrEqual(x->value.qname.uri, y->value.qname.uri)))
  4596. return(0);
  4597. return(2);
  4598. }
  4599. return (-2);
  4600. case XML_SCHEMAS_FLOAT:
  4601. case XML_SCHEMAS_DOUBLE:
  4602. if ((x == NULL) || (y == NULL))
  4603. return(-2);
  4604. if ((ytype == XML_SCHEMAS_FLOAT) ||
  4605. (ytype == XML_SCHEMAS_DOUBLE))
  4606. return (xmlSchemaCompareFloats(x, y));
  4607. return (-2);
  4608. case XML_SCHEMAS_BOOLEAN:
  4609. if ((x == NULL) || (y == NULL))
  4610. return(-2);
  4611. if (ytype == XML_SCHEMAS_BOOLEAN) {
  4612. if (x->value.b == y->value.b)
  4613. return(0);
  4614. if (x->value.b == 0)
  4615. return(-1);
  4616. return(1);
  4617. }
  4618. return (-2);
  4619. case XML_SCHEMAS_HEXBINARY:
  4620. if ((x == NULL) || (y == NULL))
  4621. return(-2);
  4622. if (ytype == XML_SCHEMAS_HEXBINARY) {
  4623. if (x->value.hex.total == y->value.hex.total) {
  4624. int ret = xmlStrcmp(x->value.hex.str, y->value.hex.str);
  4625. if (ret > 0)
  4626. return(1);
  4627. else if (ret == 0)
  4628. return(0);
  4629. }
  4630. else if (x->value.hex.total > y->value.hex.total)
  4631. return(1);
  4632. return(-1);
  4633. }
  4634. return (-2);
  4635. case XML_SCHEMAS_BASE64BINARY:
  4636. if ((x == NULL) || (y == NULL))
  4637. return(-2);
  4638. if (ytype == XML_SCHEMAS_BASE64BINARY) {
  4639. if (x->value.base64.total == y->value.base64.total) {
  4640. int ret = xmlStrcmp(x->value.base64.str,
  4641. y->value.base64.str);
  4642. if (ret > 0)
  4643. return(1);
  4644. else if (ret == 0)
  4645. return(0);
  4646. else
  4647. return(-1);
  4648. }
  4649. else if (x->value.base64.total > y->value.base64.total)
  4650. return(1);
  4651. else
  4652. return(-1);
  4653. }
  4654. return (-2);
  4655. case XML_SCHEMAS_IDREFS:
  4656. case XML_SCHEMAS_ENTITIES:
  4657. case XML_SCHEMAS_NMTOKENS:
  4658. TODO
  4659. break;
  4660. }
  4661. return -2;
  4662. }
  4663. /**
  4664. * xmlSchemaCompareValues:
  4665. * @x: a first value
  4666. * @y: a second value
  4667. *
  4668. * Compare 2 values
  4669. *
  4670. * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
  4671. * case of error
  4672. */
  4673. int
  4674. xmlSchemaCompareValues(xmlSchemaValPtr x, xmlSchemaValPtr y) {
  4675. xmlSchemaWhitespaceValueType xws, yws;
  4676. if ((x == NULL) || (y == NULL))
  4677. return(-2);
  4678. if (x->type == XML_SCHEMAS_STRING)
  4679. xws = XML_SCHEMA_WHITESPACE_PRESERVE;
  4680. else if (x->type == XML_SCHEMAS_NORMSTRING)
  4681. xws = XML_SCHEMA_WHITESPACE_REPLACE;
  4682. else
  4683. xws = XML_SCHEMA_WHITESPACE_COLLAPSE;
  4684. if (y->type == XML_SCHEMAS_STRING)
  4685. yws = XML_SCHEMA_WHITESPACE_PRESERVE;
  4686. else if (y->type == XML_SCHEMAS_NORMSTRING)
  4687. yws = XML_SCHEMA_WHITESPACE_REPLACE;
  4688. else
  4689. yws = XML_SCHEMA_WHITESPACE_COLLAPSE;
  4690. return(xmlSchemaCompareValuesInternal(x->type, x, NULL, xws, y->type,
  4691. y, NULL, yws));
  4692. }
  4693. /**
  4694. * xmlSchemaCompareValuesWhtsp:
  4695. * @x: a first value
  4696. * @xws: the whitespace value of x
  4697. * @y: a second value
  4698. * @yws: the whitespace value of y
  4699. *
  4700. * Compare 2 values
  4701. *
  4702. * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
  4703. * case of error
  4704. */
  4705. int
  4706. xmlSchemaCompareValuesWhtsp(xmlSchemaValPtr x,
  4707. xmlSchemaWhitespaceValueType xws,
  4708. xmlSchemaValPtr y,
  4709. xmlSchemaWhitespaceValueType yws)
  4710. {
  4711. if ((x == NULL) || (y == NULL))
  4712. return(-2);
  4713. return(xmlSchemaCompareValuesInternal(x->type, x, NULL, xws, y->type,
  4714. y, NULL, yws));
  4715. }
  4716. /**
  4717. * xmlSchemaCompareValuesWhtspExt:
  4718. * @x: a first value
  4719. * @xws: the whitespace value of x
  4720. * @y: a second value
  4721. * @yws: the whitespace value of y
  4722. *
  4723. * Compare 2 values
  4724. *
  4725. * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
  4726. * case of error
  4727. */
  4728. static int
  4729. xmlSchemaCompareValuesWhtspExt(xmlSchemaValType xtype,
  4730. xmlSchemaValPtr x,
  4731. const xmlChar *xvalue,
  4732. xmlSchemaWhitespaceValueType xws,
  4733. xmlSchemaValType ytype,
  4734. xmlSchemaValPtr y,
  4735. const xmlChar *yvalue,
  4736. xmlSchemaWhitespaceValueType yws)
  4737. {
  4738. return(xmlSchemaCompareValuesInternal(xtype, x, xvalue, xws, ytype, y,
  4739. yvalue, yws));
  4740. }
  4741. /**
  4742. * xmlSchemaNormLen:
  4743. * @value: a string
  4744. *
  4745. * Computes the UTF8 length of the normalized value of the string
  4746. *
  4747. * Returns the length or -1 in case of error.
  4748. */
  4749. static int
  4750. xmlSchemaNormLen(const xmlChar *value) {
  4751. const xmlChar *utf;
  4752. int ret = 0;
  4753. if (value == NULL)
  4754. return(-1);
  4755. utf = value;
  4756. while (IS_BLANK_CH(*utf)) utf++;
  4757. while (*utf != 0) {
  4758. if (utf[0] & 0x80) {
  4759. if ((utf[1] & 0xc0) != 0x80)
  4760. return(-1);
  4761. if ((utf[0] & 0xe0) == 0xe0) {
  4762. if ((utf[2] & 0xc0) != 0x80)
  4763. return(-1);
  4764. if ((utf[0] & 0xf0) == 0xf0) {
  4765. if ((utf[0] & 0xf8) != 0xf0 || (utf[3] & 0xc0) != 0x80)
  4766. return(-1);
  4767. utf += 4;
  4768. } else {
  4769. utf += 3;
  4770. }
  4771. } else {
  4772. utf += 2;
  4773. }
  4774. } else if (IS_BLANK_CH(*utf)) {
  4775. while (IS_BLANK_CH(*utf)) utf++;
  4776. if (*utf == 0)
  4777. break;
  4778. } else {
  4779. utf++;
  4780. }
  4781. ret++;
  4782. }
  4783. return(ret);
  4784. }
  4785. /**
  4786. * xmlSchemaGetFacetValueAsULong:
  4787. * @facet: an schemas type facet
  4788. *
  4789. * Extract the value of a facet
  4790. *
  4791. * Returns the value as a long
  4792. */
  4793. unsigned long
  4794. xmlSchemaGetFacetValueAsULong(xmlSchemaFacetPtr facet)
  4795. {
  4796. /*
  4797. * TODO: Check if this is a decimal.
  4798. */
  4799. if (facet == NULL || facet->val == NULL)
  4800. return 0;
  4801. return ((unsigned long) facet->val->value.decimal.lo);
  4802. }
  4803. /**
  4804. * xmlSchemaValidateListSimpleTypeFacet:
  4805. * @facet: the facet to check
  4806. * @value: the lexical repr of the value to validate
  4807. * @actualLen: the number of list items
  4808. * @expectedLen: the resulting expected number of list items
  4809. *
  4810. * Checks the value of a list simple type against a facet.
  4811. *
  4812. * Returns 0 if the value is valid, a positive error code
  4813. * number otherwise and -1 in case of an internal error.
  4814. */
  4815. int
  4816. xmlSchemaValidateListSimpleTypeFacet(xmlSchemaFacetPtr facet,
  4817. const xmlChar *value,
  4818. unsigned long actualLen,
  4819. unsigned long *expectedLen)
  4820. {
  4821. if (facet == NULL)
  4822. return(-1);
  4823. /*
  4824. * TODO: Check if this will work with large numbers.
  4825. * (compare value.decimal.mi and value.decimal.hi as well?).
  4826. */
  4827. if (facet->type == XML_SCHEMA_FACET_LENGTH) {
  4828. if (actualLen != facet->val->value.decimal.lo) {
  4829. if (expectedLen != NULL)
  4830. *expectedLen = facet->val->value.decimal.lo;
  4831. return (XML_SCHEMAV_CVC_LENGTH_VALID);
  4832. }
  4833. } else if (facet->type == XML_SCHEMA_FACET_MINLENGTH) {
  4834. if (actualLen < facet->val->value.decimal.lo) {
  4835. if (expectedLen != NULL)
  4836. *expectedLen = facet->val->value.decimal.lo;
  4837. return (XML_SCHEMAV_CVC_MINLENGTH_VALID);
  4838. }
  4839. } else if (facet->type == XML_SCHEMA_FACET_MAXLENGTH) {
  4840. if (actualLen > facet->val->value.decimal.lo) {
  4841. if (expectedLen != NULL)
  4842. *expectedLen = facet->val->value.decimal.lo;
  4843. return (XML_SCHEMAV_CVC_MAXLENGTH_VALID);
  4844. }
  4845. } else
  4846. /*
  4847. * NOTE: That we can pass NULL as xmlSchemaValPtr to
  4848. * xmlSchemaValidateFacet, since the remaining facet types
  4849. * are: XML_SCHEMA_FACET_PATTERN, XML_SCHEMA_FACET_ENUMERATION.
  4850. */
  4851. return(xmlSchemaValidateFacet(NULL, facet, value, NULL));
  4852. return (0);
  4853. }
  4854. /**
  4855. * xmlSchemaValidateLengthFacet:
  4856. * @type: the built-in type
  4857. * @facet: the facet to check
  4858. * @value: the lexical repr. of the value to be validated
  4859. * @val: the precomputed value
  4860. * @ws: the whitespace type of the value
  4861. * @length: the actual length of the value
  4862. *
  4863. * Checka a value against a "length", "minLength" and "maxLength"
  4864. * facet; sets @length to the computed length of @value.
  4865. *
  4866. * Returns 0 if the value is valid, a positive error code
  4867. * otherwise and -1 in case of an internal or API error.
  4868. */
  4869. static int
  4870. xmlSchemaValidateLengthFacetInternal(xmlSchemaFacetPtr facet,
  4871. xmlSchemaValType valType,
  4872. const xmlChar *value,
  4873. xmlSchemaValPtr val,
  4874. unsigned long *length,
  4875. xmlSchemaWhitespaceValueType ws)
  4876. {
  4877. unsigned int len = 0;
  4878. if ((length == NULL) || (facet == NULL))
  4879. return (-1);
  4880. *length = 0;
  4881. if ((facet->type != XML_SCHEMA_FACET_LENGTH) &&
  4882. (facet->type != XML_SCHEMA_FACET_MAXLENGTH) &&
  4883. (facet->type != XML_SCHEMA_FACET_MINLENGTH))
  4884. return (-1);
  4885. /*
  4886. * TODO: length, maxLength and minLength must be of type
  4887. * nonNegativeInteger only. Check if decimal is used somehow.
  4888. */
  4889. if ((facet->val == NULL) ||
  4890. ((facet->val->type != XML_SCHEMAS_DECIMAL) &&
  4891. (facet->val->type != XML_SCHEMAS_NNINTEGER)) ||
  4892. (facet->val->value.decimal.frac != 0)) {
  4893. return(-1);
  4894. }
  4895. if ((val != NULL) && (val->type == XML_SCHEMAS_HEXBINARY))
  4896. len = val->value.hex.total;
  4897. else if ((val != NULL) && (val->type == XML_SCHEMAS_BASE64BINARY))
  4898. len = val->value.base64.total;
  4899. else {
  4900. switch (valType) {
  4901. case XML_SCHEMAS_STRING:
  4902. case XML_SCHEMAS_NORMSTRING:
  4903. if (ws == XML_SCHEMA_WHITESPACE_UNKNOWN) {
  4904. /*
  4905. * This is to ensure API compatibility with the old
  4906. * xmlSchemaValidateLengthFacet(). Anyway, this was and
  4907. * is not the correct handling.
  4908. * TODO: Get rid of this case somehow.
  4909. */
  4910. if (valType == XML_SCHEMAS_STRING)
  4911. len = xmlUTF8Strlen(value);
  4912. else
  4913. len = xmlSchemaNormLen(value);
  4914. } else if (value != NULL) {
  4915. if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
  4916. len = xmlSchemaNormLen(value);
  4917. else
  4918. /*
  4919. * Should be OK for "preserve" as well.
  4920. */
  4921. len = xmlUTF8Strlen(value);
  4922. }
  4923. break;
  4924. case XML_SCHEMAS_IDREF:
  4925. case XML_SCHEMAS_TOKEN:
  4926. case XML_SCHEMAS_LANGUAGE:
  4927. case XML_SCHEMAS_NMTOKEN:
  4928. case XML_SCHEMAS_NAME:
  4929. case XML_SCHEMAS_NCNAME:
  4930. case XML_SCHEMAS_ID:
  4931. /*
  4932. * FIXME: What exactly to do with anyURI?
  4933. */
  4934. case XML_SCHEMAS_ANYURI:
  4935. if (value != NULL)
  4936. len = xmlSchemaNormLen(value);
  4937. break;
  4938. case XML_SCHEMAS_QNAME:
  4939. case XML_SCHEMAS_NOTATION:
  4940. /*
  4941. * For QName and NOTATION, those facets are
  4942. * deprecated and should be ignored.
  4943. */
  4944. return (0);
  4945. default:
  4946. TODO
  4947. }
  4948. }
  4949. *length = (unsigned long) len;
  4950. /*
  4951. * TODO: Return the whole expected value, i.e. "lo", "mi" and "hi".
  4952. */
  4953. if (facet->type == XML_SCHEMA_FACET_LENGTH) {
  4954. if (len != facet->val->value.decimal.lo)
  4955. return(XML_SCHEMAV_CVC_LENGTH_VALID);
  4956. } else if (facet->type == XML_SCHEMA_FACET_MINLENGTH) {
  4957. if (len < facet->val->value.decimal.lo)
  4958. return(XML_SCHEMAV_CVC_MINLENGTH_VALID);
  4959. } else {
  4960. if (len > facet->val->value.decimal.lo)
  4961. return(XML_SCHEMAV_CVC_MAXLENGTH_VALID);
  4962. }
  4963. return (0);
  4964. }
  4965. /**
  4966. * xmlSchemaValidateLengthFacet:
  4967. * @type: the built-in type
  4968. * @facet: the facet to check
  4969. * @value: the lexical repr. of the value to be validated
  4970. * @val: the precomputed value
  4971. * @length: the actual length of the value
  4972. *
  4973. * Checka a value against a "length", "minLength" and "maxLength"
  4974. * facet; sets @length to the computed length of @value.
  4975. *
  4976. * Returns 0 if the value is valid, a positive error code
  4977. * otherwise and -1 in case of an internal or API error.
  4978. */
  4979. int
  4980. xmlSchemaValidateLengthFacet(xmlSchemaTypePtr type,
  4981. xmlSchemaFacetPtr facet,
  4982. const xmlChar *value,
  4983. xmlSchemaValPtr val,
  4984. unsigned long *length)
  4985. {
  4986. if (type == NULL)
  4987. return(-1);
  4988. return (xmlSchemaValidateLengthFacetInternal(facet,
  4989. type->builtInType, value, val, length,
  4990. XML_SCHEMA_WHITESPACE_UNKNOWN));
  4991. }
  4992. /**
  4993. * xmlSchemaValidateLengthFacetWhtsp:
  4994. * @facet: the facet to check
  4995. * @valType: the built-in type
  4996. * @value: the lexical repr. of the value to be validated
  4997. * @val: the precomputed value
  4998. * @ws: the whitespace type of the value
  4999. * @length: the actual length of the value
  5000. *
  5001. * Checka a value against a "length", "minLength" and "maxLength"
  5002. * facet; sets @length to the computed length of @value.
  5003. *
  5004. * Returns 0 if the value is valid, a positive error code
  5005. * otherwise and -1 in case of an internal or API error.
  5006. */
  5007. int
  5008. xmlSchemaValidateLengthFacetWhtsp(xmlSchemaFacetPtr facet,
  5009. xmlSchemaValType valType,
  5010. const xmlChar *value,
  5011. xmlSchemaValPtr val,
  5012. unsigned long *length,
  5013. xmlSchemaWhitespaceValueType ws)
  5014. {
  5015. return (xmlSchemaValidateLengthFacetInternal(facet, valType, value, val,
  5016. length, ws));
  5017. }
  5018. /**
  5019. * xmlSchemaValidateFacetInternal:
  5020. * @facet: the facet to check
  5021. * @fws: the whitespace type of the facet's value
  5022. * @valType: the built-in type of the value
  5023. * @value: the lexical repr of the value to validate
  5024. * @val: the precomputed value
  5025. * @ws: the whitespace type of the value
  5026. *
  5027. * Check a value against a facet condition
  5028. *
  5029. * Returns 0 if the element is schemas valid, a positive error code
  5030. * number otherwise and -1 in case of internal or API error.
  5031. */
  5032. static int
  5033. xmlSchemaValidateFacetInternal(xmlSchemaFacetPtr facet,
  5034. xmlSchemaWhitespaceValueType fws,
  5035. xmlSchemaValType valType,
  5036. const xmlChar *value,
  5037. xmlSchemaValPtr val,
  5038. xmlSchemaWhitespaceValueType ws)
  5039. {
  5040. int ret;
  5041. int stringType;
  5042. if (facet == NULL)
  5043. return(-1);
  5044. switch (facet->type) {
  5045. case XML_SCHEMA_FACET_PATTERN:
  5046. /*
  5047. * NOTE that for patterns, the @value needs to be the normalized
  5048. * value, *not* the lexical initial value or the canonical value.
  5049. */
  5050. if (value == NULL)
  5051. return(-1);
  5052. /*
  5053. * If string-derived type, regexp must be tested on the value space of
  5054. * the datatype.
  5055. * See https://www.w3.org/TR/xmlschema-2/#rf-pattern
  5056. */
  5057. stringType = val && ((val->type >= XML_SCHEMAS_STRING && val->type <= XML_SCHEMAS_NORMSTRING)
  5058. || (val->type >= XML_SCHEMAS_TOKEN && val->type <= XML_SCHEMAS_NCNAME));
  5059. ret = xmlRegexpExec(facet->regexp,
  5060. (stringType && val->value.str) ? val->value.str : value);
  5061. if (ret == 1)
  5062. return(0);
  5063. if (ret == 0)
  5064. return(XML_SCHEMAV_CVC_PATTERN_VALID);
  5065. return(ret);
  5066. case XML_SCHEMA_FACET_MAXEXCLUSIVE:
  5067. ret = xmlSchemaCompareValues(val, facet->val);
  5068. if (ret == -2)
  5069. return(-1);
  5070. if (ret == -1)
  5071. return(0);
  5072. return(XML_SCHEMAV_CVC_MAXEXCLUSIVE_VALID);
  5073. case XML_SCHEMA_FACET_MAXINCLUSIVE:
  5074. ret = xmlSchemaCompareValues(val, facet->val);
  5075. if (ret == -2)
  5076. return(-1);
  5077. if ((ret == -1) || (ret == 0))
  5078. return(0);
  5079. return(XML_SCHEMAV_CVC_MAXINCLUSIVE_VALID);
  5080. case XML_SCHEMA_FACET_MINEXCLUSIVE:
  5081. ret = xmlSchemaCompareValues(val, facet->val);
  5082. if (ret == -2)
  5083. return(-1);
  5084. if (ret == 1)
  5085. return(0);
  5086. return(XML_SCHEMAV_CVC_MINEXCLUSIVE_VALID);
  5087. case XML_SCHEMA_FACET_MININCLUSIVE:
  5088. ret = xmlSchemaCompareValues(val, facet->val);
  5089. if (ret == -2)
  5090. return(-1);
  5091. if ((ret == 1) || (ret == 0))
  5092. return(0);
  5093. return(XML_SCHEMAV_CVC_MININCLUSIVE_VALID);
  5094. case XML_SCHEMA_FACET_WHITESPACE:
  5095. /* TODO whitespaces */
  5096. /*
  5097. * NOTE: Whitespace should be handled to normalize
  5098. * the value to be validated against a the facets;
  5099. * not to normalize the value in-between.
  5100. */
  5101. return(0);
  5102. case XML_SCHEMA_FACET_ENUMERATION:
  5103. if (ws == XML_SCHEMA_WHITESPACE_UNKNOWN) {
  5104. /*
  5105. * This is to ensure API compatibility with the old
  5106. * xmlSchemaValidateFacet().
  5107. * TODO: Get rid of this case.
  5108. */
  5109. if ((facet->value != NULL) &&
  5110. (xmlStrEqual(facet->value, value)))
  5111. return(0);
  5112. } else {
  5113. ret = xmlSchemaCompareValuesWhtspExt(facet->val->type,
  5114. facet->val, facet->value, fws, valType, val,
  5115. value, ws);
  5116. if (ret == -2)
  5117. return(-1);
  5118. if (ret == 0)
  5119. return(0);
  5120. }
  5121. return(XML_SCHEMAV_CVC_ENUMERATION_VALID);
  5122. case XML_SCHEMA_FACET_LENGTH:
  5123. /*
  5124. * SPEC (1.3) "if {primitive type definition} is QName or NOTATION,
  5125. * then any {value} is facet-valid."
  5126. */
  5127. if ((valType == XML_SCHEMAS_QNAME) ||
  5128. (valType == XML_SCHEMAS_NOTATION))
  5129. return (0);
  5130. /* Falls through. */
  5131. case XML_SCHEMA_FACET_MAXLENGTH:
  5132. case XML_SCHEMA_FACET_MINLENGTH: {
  5133. unsigned int len = 0;
  5134. if ((valType == XML_SCHEMAS_QNAME) ||
  5135. (valType == XML_SCHEMAS_NOTATION))
  5136. return (0);
  5137. /*
  5138. * TODO: length, maxLength and minLength must be of type
  5139. * nonNegativeInteger only. Check if decimal is used somehow.
  5140. */
  5141. if ((facet->val == NULL) ||
  5142. ((facet->val->type != XML_SCHEMAS_DECIMAL) &&
  5143. (facet->val->type != XML_SCHEMAS_NNINTEGER)) ||
  5144. (facet->val->value.decimal.frac != 0)) {
  5145. return(-1);
  5146. }
  5147. if ((val != NULL) && (val->type == XML_SCHEMAS_HEXBINARY))
  5148. len = val->value.hex.total;
  5149. else if ((val != NULL) && (val->type == XML_SCHEMAS_BASE64BINARY))
  5150. len = val->value.base64.total;
  5151. else {
  5152. switch (valType) {
  5153. case XML_SCHEMAS_STRING:
  5154. case XML_SCHEMAS_NORMSTRING:
  5155. if (ws == XML_SCHEMA_WHITESPACE_UNKNOWN) {
  5156. /*
  5157. * This is to ensure API compatibility with the old
  5158. * xmlSchemaValidateFacet(). Anyway, this was and
  5159. * is not the correct handling.
  5160. * TODO: Get rid of this case somehow.
  5161. */
  5162. if (valType == XML_SCHEMAS_STRING)
  5163. len = xmlUTF8Strlen(value);
  5164. else
  5165. len = xmlSchemaNormLen(value);
  5166. } else if (value != NULL) {
  5167. if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
  5168. len = xmlSchemaNormLen(value);
  5169. else
  5170. /*
  5171. * Should be OK for "preserve" as well.
  5172. */
  5173. len = xmlUTF8Strlen(value);
  5174. }
  5175. break;
  5176. case XML_SCHEMAS_IDREF:
  5177. case XML_SCHEMAS_TOKEN:
  5178. case XML_SCHEMAS_LANGUAGE:
  5179. case XML_SCHEMAS_NMTOKEN:
  5180. case XML_SCHEMAS_NAME:
  5181. case XML_SCHEMAS_NCNAME:
  5182. case XML_SCHEMAS_ID:
  5183. case XML_SCHEMAS_ANYURI:
  5184. if (value != NULL)
  5185. len = xmlSchemaNormLen(value);
  5186. break;
  5187. default:
  5188. TODO
  5189. }
  5190. }
  5191. if (facet->type == XML_SCHEMA_FACET_LENGTH) {
  5192. if (len != facet->val->value.decimal.lo)
  5193. return(XML_SCHEMAV_CVC_LENGTH_VALID);
  5194. } else if (facet->type == XML_SCHEMA_FACET_MINLENGTH) {
  5195. if (len < facet->val->value.decimal.lo)
  5196. return(XML_SCHEMAV_CVC_MINLENGTH_VALID);
  5197. } else {
  5198. if (len > facet->val->value.decimal.lo)
  5199. return(XML_SCHEMAV_CVC_MAXLENGTH_VALID);
  5200. }
  5201. break;
  5202. }
  5203. case XML_SCHEMA_FACET_TOTALDIGITS:
  5204. case XML_SCHEMA_FACET_FRACTIONDIGITS:
  5205. if ((facet->val == NULL) ||
  5206. ((facet->val->type != XML_SCHEMAS_PINTEGER) &&
  5207. (facet->val->type != XML_SCHEMAS_NNINTEGER)) ||
  5208. (facet->val->value.decimal.frac != 0)) {
  5209. return(-1);
  5210. }
  5211. if ((val == NULL) ||
  5212. ((val->type != XML_SCHEMAS_DECIMAL) &&
  5213. (val->type != XML_SCHEMAS_INTEGER) &&
  5214. (val->type != XML_SCHEMAS_NPINTEGER) &&
  5215. (val->type != XML_SCHEMAS_NINTEGER) &&
  5216. (val->type != XML_SCHEMAS_NNINTEGER) &&
  5217. (val->type != XML_SCHEMAS_PINTEGER) &&
  5218. (val->type != XML_SCHEMAS_INT) &&
  5219. (val->type != XML_SCHEMAS_UINT) &&
  5220. (val->type != XML_SCHEMAS_LONG) &&
  5221. (val->type != XML_SCHEMAS_ULONG) &&
  5222. (val->type != XML_SCHEMAS_SHORT) &&
  5223. (val->type != XML_SCHEMAS_USHORT) &&
  5224. (val->type != XML_SCHEMAS_BYTE) &&
  5225. (val->type != XML_SCHEMAS_UBYTE))) {
  5226. return(-1);
  5227. }
  5228. if (facet->type == XML_SCHEMA_FACET_TOTALDIGITS) {
  5229. if (val->value.decimal.total > facet->val->value.decimal.lo)
  5230. return(XML_SCHEMAV_CVC_TOTALDIGITS_VALID);
  5231. } else if (facet->type == XML_SCHEMA_FACET_FRACTIONDIGITS) {
  5232. if (val->value.decimal.frac > facet->val->value.decimal.lo)
  5233. return(XML_SCHEMAV_CVC_FRACTIONDIGITS_VALID);
  5234. }
  5235. break;
  5236. default:
  5237. TODO
  5238. }
  5239. return(0);
  5240. }
  5241. /**
  5242. * xmlSchemaValidateFacet:
  5243. * @base: the base type
  5244. * @facet: the facet to check
  5245. * @value: the lexical repr of the value to validate
  5246. * @val: the precomputed value
  5247. *
  5248. * Check a value against a facet condition
  5249. *
  5250. * Returns 0 if the element is schemas valid, a positive error code
  5251. * number otherwise and -1 in case of internal or API error.
  5252. */
  5253. int
  5254. xmlSchemaValidateFacet(xmlSchemaTypePtr base,
  5255. xmlSchemaFacetPtr facet,
  5256. const xmlChar *value,
  5257. xmlSchemaValPtr val)
  5258. {
  5259. /*
  5260. * This tries to ensure API compatibility regarding the old
  5261. * xmlSchemaValidateFacet() and the new xmlSchemaValidateFacetInternal() and
  5262. * xmlSchemaValidateFacetWhtsp().
  5263. */
  5264. if (val != NULL)
  5265. return(xmlSchemaValidateFacetInternal(facet,
  5266. XML_SCHEMA_WHITESPACE_UNKNOWN, val->type, value, val,
  5267. XML_SCHEMA_WHITESPACE_UNKNOWN));
  5268. else if (base != NULL)
  5269. return(xmlSchemaValidateFacetInternal(facet,
  5270. XML_SCHEMA_WHITESPACE_UNKNOWN, base->builtInType, value, val,
  5271. XML_SCHEMA_WHITESPACE_UNKNOWN));
  5272. return(-1);
  5273. }
  5274. /**
  5275. * xmlSchemaValidateFacetWhtsp:
  5276. * @facet: the facet to check
  5277. * @fws: the whitespace type of the facet's value
  5278. * @valType: the built-in type of the value
  5279. * @value: the lexical (or normalized for pattern) repr of the value to validate
  5280. * @val: the precomputed value
  5281. * @ws: the whitespace type of the value
  5282. *
  5283. * Check a value against a facet condition. This takes value normalization
  5284. * according to the specified whitespace types into account.
  5285. * Note that @value needs to be the *normalized* value if the facet
  5286. * is of type "pattern".
  5287. *
  5288. * Returns 0 if the element is schemas valid, a positive error code
  5289. * number otherwise and -1 in case of internal or API error.
  5290. */
  5291. int
  5292. xmlSchemaValidateFacetWhtsp(xmlSchemaFacetPtr facet,
  5293. xmlSchemaWhitespaceValueType fws,
  5294. xmlSchemaValType valType,
  5295. const xmlChar *value,
  5296. xmlSchemaValPtr val,
  5297. xmlSchemaWhitespaceValueType ws)
  5298. {
  5299. return(xmlSchemaValidateFacetInternal(facet, fws, valType,
  5300. value, val, ws));
  5301. }
  5302. #if 0
  5303. #ifndef DBL_DIG
  5304. #define DBL_DIG 16
  5305. #endif
  5306. #ifndef DBL_EPSILON
  5307. #define DBL_EPSILON 1E-9
  5308. #endif
  5309. #define INTEGER_DIGITS DBL_DIG
  5310. #define FRACTION_DIGITS (DBL_DIG + 1)
  5311. #define EXPONENT_DIGITS (3 + 2)
  5312. /**
  5313. * xmlXPathFormatNumber:
  5314. * @number: number to format
  5315. * @buffer: output buffer
  5316. * @buffersize: size of output buffer
  5317. *
  5318. * Convert the number into a string representation.
  5319. */
  5320. static void
  5321. xmlSchemaFormatFloat(double number, char buffer[], int buffersize)
  5322. {
  5323. switch (xmlXPathIsInf(number)) {
  5324. case 1:
  5325. if (buffersize > (int)sizeof("INF"))
  5326. snprintf(buffer, buffersize, "INF");
  5327. break;
  5328. case -1:
  5329. if (buffersize > (int)sizeof("-INF"))
  5330. snprintf(buffer, buffersize, "-INF");
  5331. break;
  5332. default:
  5333. if (xmlXPathIsNaN(number)) {
  5334. if (buffersize > (int)sizeof("NaN"))
  5335. snprintf(buffer, buffersize, "NaN");
  5336. } else if (number == 0) {
  5337. snprintf(buffer, buffersize, "0.0E0");
  5338. } else {
  5339. /* 3 is sign, decimal point, and terminating zero */
  5340. char work[DBL_DIG + EXPONENT_DIGITS + 3];
  5341. int integer_place, fraction_place;
  5342. char *ptr;
  5343. char *after_fraction;
  5344. double absolute_value;
  5345. int size;
  5346. absolute_value = fabs(number);
  5347. /*
  5348. * Result is in work, and after_fraction points
  5349. * just past the fractional part.
  5350. * Use scientific notation
  5351. */
  5352. integer_place = DBL_DIG + EXPONENT_DIGITS + 1;
  5353. fraction_place = DBL_DIG - 1;
  5354. snprintf(work, sizeof(work),"%*.*e",
  5355. integer_place, fraction_place, number);
  5356. after_fraction = strchr(work + DBL_DIG, 'e');
  5357. /* Remove fractional trailing zeroes */
  5358. ptr = after_fraction;
  5359. while (*(--ptr) == '0')
  5360. ;
  5361. if (*ptr != '.')
  5362. ptr++;
  5363. while ((*ptr++ = *after_fraction++) != 0);
  5364. /* Finally copy result back to caller */
  5365. size = strlen(work) + 1;
  5366. if (size > buffersize) {
  5367. work[buffersize - 1] = 0;
  5368. size = buffersize;
  5369. }
  5370. memmove(buffer, work, size);
  5371. }
  5372. break;
  5373. }
  5374. }
  5375. #endif
  5376. /**
  5377. * xmlSchemaGetCanonValue:
  5378. * @val: the precomputed value
  5379. * @retValue: the returned value
  5380. *
  5381. * Get the canonical lexical representation of the value.
  5382. * The caller has to FREE the returned retValue.
  5383. *
  5384. * WARNING: Some value types are not supported yet, resulting
  5385. * in a @retValue of "???".
  5386. *
  5387. * TODO: XML Schema 1.0 does not define canonical representations
  5388. * for: duration, gYearMonth, gYear, gMonthDay, gMonth, gDay,
  5389. * anyURI, QName, NOTATION. This will be fixed in XML Schema 1.1.
  5390. *
  5391. *
  5392. * Returns 0 if the value could be built, 1 if the value type is
  5393. * not supported yet and -1 in case of API errors.
  5394. */
  5395. int
  5396. xmlSchemaGetCanonValue(xmlSchemaValPtr val, const xmlChar **retValue)
  5397. {
  5398. if ((retValue == NULL) || (val == NULL))
  5399. return (-1);
  5400. *retValue = NULL;
  5401. switch (val->type) {
  5402. case XML_SCHEMAS_STRING:
  5403. if (val->value.str == NULL)
  5404. *retValue = BAD_CAST xmlStrdup(BAD_CAST "");
  5405. else
  5406. *retValue =
  5407. BAD_CAST xmlStrdup((const xmlChar *) val->value.str);
  5408. break;
  5409. case XML_SCHEMAS_NORMSTRING:
  5410. if (val->value.str == NULL)
  5411. *retValue = BAD_CAST xmlStrdup(BAD_CAST "");
  5412. else {
  5413. *retValue = xmlSchemaWhiteSpaceReplace(
  5414. (const xmlChar *) val->value.str);
  5415. if ((*retValue) == NULL)
  5416. *retValue = BAD_CAST xmlStrdup(
  5417. (const xmlChar *) val->value.str);
  5418. }
  5419. break;
  5420. case XML_SCHEMAS_TOKEN:
  5421. case XML_SCHEMAS_LANGUAGE:
  5422. case XML_SCHEMAS_NMTOKEN:
  5423. case XML_SCHEMAS_NAME:
  5424. case XML_SCHEMAS_NCNAME:
  5425. case XML_SCHEMAS_ID:
  5426. case XML_SCHEMAS_IDREF:
  5427. case XML_SCHEMAS_ENTITY:
  5428. case XML_SCHEMAS_NOTATION: /* Unclear */
  5429. case XML_SCHEMAS_ANYURI: /* Unclear */
  5430. if (val->value.str == NULL)
  5431. return (-1);
  5432. *retValue =
  5433. BAD_CAST xmlSchemaCollapseString(BAD_CAST val->value.str);
  5434. if (*retValue == NULL)
  5435. *retValue =
  5436. BAD_CAST xmlStrdup((const xmlChar *) val->value.str);
  5437. break;
  5438. case XML_SCHEMAS_QNAME:
  5439. /* TODO: Unclear in XML Schema 1.0. */
  5440. if (val->value.qname.uri == NULL) {
  5441. *retValue = BAD_CAST xmlStrdup(BAD_CAST val->value.qname.name);
  5442. return (0);
  5443. } else {
  5444. *retValue = BAD_CAST xmlStrdup(BAD_CAST "{");
  5445. *retValue = BAD_CAST xmlStrcat((xmlChar *) (*retValue),
  5446. BAD_CAST val->value.qname.uri);
  5447. *retValue = BAD_CAST xmlStrcat((xmlChar *) (*retValue),
  5448. BAD_CAST "}");
  5449. *retValue = BAD_CAST xmlStrcat((xmlChar *) (*retValue),
  5450. BAD_CAST val->value.qname.uri);
  5451. }
  5452. break;
  5453. case XML_SCHEMAS_DECIMAL:
  5454. /*
  5455. * TODO: Lookout for a more simple implementation.
  5456. */
  5457. if ((val->value.decimal.total == 1) &&
  5458. (val->value.decimal.lo == 0)) {
  5459. *retValue = xmlStrdup(BAD_CAST "0.0");
  5460. } else {
  5461. xmlSchemaValDecimal dec = val->value.decimal;
  5462. int bufsize;
  5463. char *buf = NULL, *offs;
  5464. /* Add room for the decimal point as well. */
  5465. bufsize = dec.total + 2;
  5466. if (dec.sign)
  5467. bufsize++;
  5468. /* Add room for leading/trailing zero. */
  5469. if ((dec.frac == 0) || (dec.frac == dec.total))
  5470. bufsize++;
  5471. buf = xmlMalloc(bufsize);
  5472. if (buf == NULL)
  5473. return(-1);
  5474. offs = buf;
  5475. if (dec.sign)
  5476. *offs++ = '-';
  5477. if (dec.frac == dec.total) {
  5478. *offs++ = '0';
  5479. *offs++ = '.';
  5480. }
  5481. if (dec.hi != 0)
  5482. snprintf(offs, bufsize - (offs - buf),
  5483. "%lu%lu%lu", dec.hi, dec.mi, dec.lo);
  5484. else if (dec.mi != 0)
  5485. snprintf(offs, bufsize - (offs - buf),
  5486. "%lu%lu", dec.mi, dec.lo);
  5487. else
  5488. snprintf(offs, bufsize - (offs - buf),
  5489. "%lu", dec.lo);
  5490. if (dec.frac != 0) {
  5491. if (dec.frac != dec.total) {
  5492. int diff = dec.total - dec.frac;
  5493. /*
  5494. * Insert the decimal point.
  5495. */
  5496. memmove(offs + diff + 1, offs + diff, dec.frac +1);
  5497. offs[diff] = '.';
  5498. } else {
  5499. unsigned int i = 0;
  5500. /*
  5501. * Insert missing zeroes behind the decimal point.
  5502. */
  5503. while (*(offs + i) != 0)
  5504. i++;
  5505. if (i < dec.total) {
  5506. memmove(offs + (dec.total - i), offs, i +1);
  5507. memset(offs, '0', dec.total - i);
  5508. }
  5509. }
  5510. } else {
  5511. /*
  5512. * Append decimal point and zero.
  5513. */
  5514. offs = buf + bufsize - 1;
  5515. *offs-- = 0;
  5516. *offs-- = '0';
  5517. *offs-- = '.';
  5518. }
  5519. *retValue = BAD_CAST buf;
  5520. }
  5521. break;
  5522. case XML_SCHEMAS_INTEGER:
  5523. case XML_SCHEMAS_PINTEGER:
  5524. case XML_SCHEMAS_NPINTEGER:
  5525. case XML_SCHEMAS_NINTEGER:
  5526. case XML_SCHEMAS_NNINTEGER:
  5527. case XML_SCHEMAS_LONG:
  5528. case XML_SCHEMAS_BYTE:
  5529. case XML_SCHEMAS_SHORT:
  5530. case XML_SCHEMAS_INT:
  5531. case XML_SCHEMAS_UINT:
  5532. case XML_SCHEMAS_ULONG:
  5533. case XML_SCHEMAS_USHORT:
  5534. case XML_SCHEMAS_UBYTE:
  5535. if ((val->value.decimal.total == 1) &&
  5536. (val->value.decimal.lo == 0))
  5537. *retValue = xmlStrdup(BAD_CAST "0");
  5538. else {
  5539. xmlSchemaValDecimal dec = val->value.decimal;
  5540. int bufsize = dec.total + 1;
  5541. /* Add room for the decimal point as well. */
  5542. if (dec.sign)
  5543. bufsize++;
  5544. *retValue = xmlMalloc(bufsize);
  5545. if (*retValue == NULL)
  5546. return(-1);
  5547. if (dec.hi != 0) {
  5548. if (dec.sign)
  5549. snprintf((char *) *retValue, bufsize,
  5550. "-%lu%lu%lu", dec.hi, dec.mi, dec.lo);
  5551. else
  5552. snprintf((char *) *retValue, bufsize,
  5553. "%lu%lu%lu", dec.hi, dec.mi, dec.lo);
  5554. } else if (dec.mi != 0) {
  5555. if (dec.sign)
  5556. snprintf((char *) *retValue, bufsize,
  5557. "-%lu%lu", dec.mi, dec.lo);
  5558. else
  5559. snprintf((char *) *retValue, bufsize,
  5560. "%lu%lu", dec.mi, dec.lo);
  5561. } else {
  5562. if (dec.sign)
  5563. snprintf((char *) *retValue, bufsize, "-%lu", dec.lo);
  5564. else
  5565. snprintf((char *) *retValue, bufsize, "%lu", dec.lo);
  5566. }
  5567. }
  5568. break;
  5569. case XML_SCHEMAS_BOOLEAN:
  5570. if (val->value.b)
  5571. *retValue = BAD_CAST xmlStrdup(BAD_CAST "true");
  5572. else
  5573. *retValue = BAD_CAST xmlStrdup(BAD_CAST "false");
  5574. break;
  5575. case XML_SCHEMAS_DURATION: {
  5576. char buf[100];
  5577. unsigned long year;
  5578. unsigned long mon, day, hour = 0, min = 0;
  5579. double sec = 0, left;
  5580. /* TODO: Unclear in XML Schema 1.0 */
  5581. /*
  5582. * TODO: This results in a normalized output of the value
  5583. * - which is NOT conformant to the spec -
  5584. * since the exact values of each property are not
  5585. * recoverable. Think about extending the structure to
  5586. * provide a field for every property.
  5587. */
  5588. year = (unsigned long) FQUOTIENT(labs(val->value.dur.mon), 12);
  5589. mon = labs(val->value.dur.mon) - 12 * year;
  5590. day = (unsigned long) FQUOTIENT(fabs(val->value.dur.sec), 86400);
  5591. left = fabs(val->value.dur.sec) - day * 86400;
  5592. if (left > 0) {
  5593. hour = (unsigned long) FQUOTIENT(left, 3600);
  5594. left = left - (hour * 3600);
  5595. if (left > 0) {
  5596. min = (unsigned long) FQUOTIENT(left, 60);
  5597. sec = left - (min * 60);
  5598. }
  5599. }
  5600. if ((val->value.dur.mon < 0) || (val->value.dur.sec < 0))
  5601. snprintf(buf, 100, "P%luY%luM%luDT%luH%luM%.14gS",
  5602. year, mon, day, hour, min, sec);
  5603. else
  5604. snprintf(buf, 100, "-P%luY%luM%luDT%luH%luM%.14gS",
  5605. year, mon, day, hour, min, sec);
  5606. *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
  5607. }
  5608. break;
  5609. case XML_SCHEMAS_GYEAR: {
  5610. char buf[30];
  5611. /* TODO: Unclear in XML Schema 1.0 */
  5612. /* TODO: What to do with the timezone? */
  5613. snprintf(buf, 30, "%04ld", val->value.date.year);
  5614. *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
  5615. }
  5616. break;
  5617. case XML_SCHEMAS_GMONTH: {
  5618. /* TODO: Unclear in XML Schema 1.0 */
  5619. /* TODO: What to do with the timezone? */
  5620. *retValue = xmlMalloc(6);
  5621. if (*retValue == NULL)
  5622. return(-1);
  5623. snprintf((char *) *retValue, 6, "--%02u",
  5624. val->value.date.mon);
  5625. }
  5626. break;
  5627. case XML_SCHEMAS_GDAY: {
  5628. /* TODO: Unclear in XML Schema 1.0 */
  5629. /* TODO: What to do with the timezone? */
  5630. *retValue = xmlMalloc(6);
  5631. if (*retValue == NULL)
  5632. return(-1);
  5633. snprintf((char *) *retValue, 6, "---%02u",
  5634. val->value.date.day);
  5635. }
  5636. break;
  5637. case XML_SCHEMAS_GMONTHDAY: {
  5638. /* TODO: Unclear in XML Schema 1.0 */
  5639. /* TODO: What to do with the timezone? */
  5640. *retValue = xmlMalloc(8);
  5641. if (*retValue == NULL)
  5642. return(-1);
  5643. snprintf((char *) *retValue, 8, "--%02u-%02u",
  5644. val->value.date.mon, val->value.date.day);
  5645. }
  5646. break;
  5647. case XML_SCHEMAS_GYEARMONTH: {
  5648. char buf[35];
  5649. /* TODO: Unclear in XML Schema 1.0 */
  5650. /* TODO: What to do with the timezone? */
  5651. if (val->value.date.year < 0)
  5652. snprintf(buf, 35, "-%04ld-%02u",
  5653. labs(val->value.date.year),
  5654. val->value.date.mon);
  5655. else
  5656. snprintf(buf, 35, "%04ld-%02u",
  5657. val->value.date.year, val->value.date.mon);
  5658. *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
  5659. }
  5660. break;
  5661. case XML_SCHEMAS_TIME:
  5662. {
  5663. char buf[30];
  5664. if (val->value.date.tz_flag) {
  5665. xmlSchemaValPtr norm;
  5666. norm = xmlSchemaDateNormalize(val, 0);
  5667. if (norm == NULL)
  5668. return (-1);
  5669. /*
  5670. * TODO: Check if "%.14g" is portable.
  5671. */
  5672. snprintf(buf, 30,
  5673. "%02u:%02u:%02.14gZ",
  5674. norm->value.date.hour,
  5675. norm->value.date.min,
  5676. norm->value.date.sec);
  5677. xmlSchemaFreeValue(norm);
  5678. } else {
  5679. snprintf(buf, 30,
  5680. "%02u:%02u:%02.14g",
  5681. val->value.date.hour,
  5682. val->value.date.min,
  5683. val->value.date.sec);
  5684. }
  5685. *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
  5686. }
  5687. break;
  5688. case XML_SCHEMAS_DATE:
  5689. {
  5690. char buf[30];
  5691. if (val->value.date.tz_flag) {
  5692. xmlSchemaValPtr norm;
  5693. norm = xmlSchemaDateNormalize(val, 0);
  5694. if (norm == NULL)
  5695. return (-1);
  5696. /*
  5697. * TODO: Append the canonical value of the
  5698. * recoverable timezone and not "Z".
  5699. */
  5700. snprintf(buf, 30,
  5701. "%04ld-%02u-%02uZ",
  5702. norm->value.date.year, norm->value.date.mon,
  5703. norm->value.date.day);
  5704. xmlSchemaFreeValue(norm);
  5705. } else {
  5706. snprintf(buf, 30,
  5707. "%04ld-%02u-%02u",
  5708. val->value.date.year, val->value.date.mon,
  5709. val->value.date.day);
  5710. }
  5711. *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
  5712. }
  5713. break;
  5714. case XML_SCHEMAS_DATETIME:
  5715. {
  5716. char buf[50];
  5717. if (val->value.date.tz_flag) {
  5718. xmlSchemaValPtr norm;
  5719. norm = xmlSchemaDateNormalize(val, 0);
  5720. if (norm == NULL)
  5721. return (-1);
  5722. /*
  5723. * TODO: Check if "%.14g" is portable.
  5724. */
  5725. snprintf(buf, 50,
  5726. "%04ld-%02u-%02uT%02u:%02u:%02.14gZ",
  5727. norm->value.date.year, norm->value.date.mon,
  5728. norm->value.date.day, norm->value.date.hour,
  5729. norm->value.date.min, norm->value.date.sec);
  5730. xmlSchemaFreeValue(norm);
  5731. } else {
  5732. snprintf(buf, 50,
  5733. "%04ld-%02u-%02uT%02u:%02u:%02.14g",
  5734. val->value.date.year, val->value.date.mon,
  5735. val->value.date.day, val->value.date.hour,
  5736. val->value.date.min, val->value.date.sec);
  5737. }
  5738. *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
  5739. }
  5740. break;
  5741. case XML_SCHEMAS_HEXBINARY:
  5742. *retValue = BAD_CAST xmlStrdup(BAD_CAST val->value.hex.str);
  5743. break;
  5744. case XML_SCHEMAS_BASE64BINARY:
  5745. /*
  5746. * TODO: Is the following spec piece implemented?:
  5747. * SPEC: "Note: For some values the canonical form defined
  5748. * above does not conform to [RFC 2045], which requires breaking
  5749. * with linefeeds at appropriate intervals."
  5750. */
  5751. *retValue = BAD_CAST xmlStrdup(BAD_CAST val->value.base64.str);
  5752. break;
  5753. case XML_SCHEMAS_FLOAT: {
  5754. char buf[30];
  5755. /*
  5756. * |m| < 16777216, -149 <= e <= 104.
  5757. * TODO: Handle, NaN, INF, -INF. The format is not
  5758. * yet conformant. The c type float does not cover
  5759. * the whole range.
  5760. */
  5761. snprintf(buf, 30, "%01.14e", val->value.f);
  5762. *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
  5763. }
  5764. break;
  5765. case XML_SCHEMAS_DOUBLE: {
  5766. char buf[40];
  5767. /* |m| < 9007199254740992, -1075 <= e <= 970 */
  5768. /*
  5769. * TODO: Handle, NaN, INF, -INF. The format is not
  5770. * yet conformant. The c type float does not cover
  5771. * the whole range.
  5772. */
  5773. snprintf(buf, 40, "%01.14e", val->value.d);
  5774. *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
  5775. }
  5776. break;
  5777. default:
  5778. *retValue = BAD_CAST xmlStrdup(BAD_CAST "???");
  5779. return (1);
  5780. }
  5781. if (*retValue == NULL)
  5782. return(-1);
  5783. return (0);
  5784. }
  5785. /**
  5786. * xmlSchemaGetCanonValueWhtsp:
  5787. * @val: the precomputed value
  5788. * @retValue: the returned value
  5789. * @ws: the whitespace type of the value
  5790. *
  5791. * Get the canonical representation of the value.
  5792. * The caller has to free the returned @retValue.
  5793. *
  5794. * Returns 0 if the value could be built, 1 if the value type is
  5795. * not supported yet and -1 in case of API errors.
  5796. */
  5797. int
  5798. xmlSchemaGetCanonValueWhtsp(xmlSchemaValPtr val,
  5799. const xmlChar **retValue,
  5800. xmlSchemaWhitespaceValueType ws)
  5801. {
  5802. if ((retValue == NULL) || (val == NULL))
  5803. return (-1);
  5804. if ((ws == XML_SCHEMA_WHITESPACE_UNKNOWN) ||
  5805. (ws > XML_SCHEMA_WHITESPACE_COLLAPSE))
  5806. return (-1);
  5807. *retValue = NULL;
  5808. switch (val->type) {
  5809. case XML_SCHEMAS_STRING:
  5810. if (val->value.str == NULL)
  5811. *retValue = BAD_CAST xmlStrdup(BAD_CAST "");
  5812. else if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
  5813. *retValue = xmlSchemaCollapseString(val->value.str);
  5814. else if (ws == XML_SCHEMA_WHITESPACE_REPLACE)
  5815. *retValue = xmlSchemaWhiteSpaceReplace(val->value.str);
  5816. if ((*retValue) == NULL)
  5817. *retValue = BAD_CAST xmlStrdup(val->value.str);
  5818. break;
  5819. case XML_SCHEMAS_NORMSTRING:
  5820. if (val->value.str == NULL)
  5821. *retValue = BAD_CAST xmlStrdup(BAD_CAST "");
  5822. else {
  5823. if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
  5824. *retValue = xmlSchemaCollapseString(val->value.str);
  5825. else
  5826. *retValue = xmlSchemaWhiteSpaceReplace(val->value.str);
  5827. if ((*retValue) == NULL)
  5828. *retValue = BAD_CAST xmlStrdup(val->value.str);
  5829. }
  5830. break;
  5831. default:
  5832. return (xmlSchemaGetCanonValue(val, retValue));
  5833. }
  5834. return (0);
  5835. }
  5836. /**
  5837. * xmlSchemaGetValType:
  5838. * @val: a schemas value
  5839. *
  5840. * Accessor for the type of a value
  5841. *
  5842. * Returns the xmlSchemaValType of the value
  5843. */
  5844. xmlSchemaValType
  5845. xmlSchemaGetValType(xmlSchemaValPtr val)
  5846. {
  5847. if (val == NULL)
  5848. return(XML_SCHEMAS_UNKNOWN);
  5849. return (val->type);
  5850. }
  5851. #define bottom_xmlschemastypes
  5852. #include "elfgcchack.h"
  5853. #endif /* LIBXML_SCHEMAS_ENABLED */