xmlschemastypes.c 173 KB

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